Discussion:
How to programmatically set a tunable parameter?
(too old to reply)
Christopher Perry
2007-04-03 14:28:18 UTC
Permalink
In Simulink, if you set the inline option in the
Configuration/Optimization for your model, you can make a parameter
tunable by hitting the Configure button and adding it to the Global
(tunable) parameters list.

I want to perform the same functionality programmatically
yet I have been unable to find a Matlab command to do this?

Can this be done programmatically?
Phil Goddard
2007-04-04 02:30:14 UTC
Permalink
There are a couple of ways to tackle this.

A. If you are using straight MATLAB variables for your parameters,
e.g. you say K1 = MyValue to create the parameters, then you need to
set the following 2 model properties.
set_param(gcs,'TunableVars','K1,K2,K3,K4');
set_param(gcs,'TunableVarsStorageClass','Auto,Auto,Auto,Auto');

If you go to the inline/tunable parameters dialog you'll see that the
parameters now appear just if you'd gone to that dialog to define
them as tunable.

Have a look at the doc for set_param if you're not familiar with that
function.

B. Alternatively, if you are using Simulink.Parameter objects to
create the variables then you need to set their RTWInfo.StorageClass
property to be anything other than auto.
Have a look in the doc for an explanation of how to do this, and what
to set the parameter to.

Phil.
Post by Christopher Perry
In Simulink, if you set the inline option in the
Configuration/Optimization for your model, you can make a parameter
tunable by hitting the Configure button and adding it to the Global
(tunable) parameters list.
I want to perform the same functionality programmatically
yet I have been unable to find a Matlab command to do this?
Can this be done programmatically?
Chris Perry
2007-04-04 11:45:23 UTC
Permalink
Sorry Phil it didn't work for me.
At the Matlab command window I issue the following:

tpTest = 1
set_param(gcs,'TunableVars','tpTest')

I get an error message - SubSystem block does not have a parameter
named 'TunableVars'

Also, once I add a parameter to the tunable parameters table, how do
I programmatically remove it?

Thanks,

-Chris
Post by Phil Goddard
There are a couple of ways to tackle this.
A. If you are using straight MATLAB variables for your parameters,
e.g. you say K1 = MyValue to create the parameters, then you need to
set the following 2 model properties.
set_param(gcs,'TunableVars','K1,K2,K3,K4');
set_param(gcs,'TunableVarsStorageClass','Auto,Auto,Auto,Auto');
If you go to the inline/tunable parameters dialog you'll see that the
parameters now appear just if you'd gone to that dialog to define
them as tunable.
Have a look at the doc for set_param if you're not familiar with that
function.
B. Alternatively, if you are using Simulink.Parameter objects to
create the variables then you need to set their
RTWInfo.StorageClass
property to be anything other than auto.
Have a look in the doc for an explanation of how to do this, and what
to set the parameter to.
Phil.
Post by Christopher Perry
In Simulink, if you set the inline option in the
Configuration/Optimization for your model, you can make a
parameter
Post by Christopher Perry
tunable by hitting the Configure button and adding it to the
Global
Post by Christopher Perry
(tunable) parameters list.
I want to perform the same functionality programmatically
yet I have been unable to find a Matlab command to do this?
Can this be done programmatically?
Chris Perry
2007-04-04 14:40:31 UTC
Permalink
Here is the solution:

% Make the variable a tunable parameter
sTuneParms = get_param(bdroot, 'TunableVars');

% Append new variable to list of tunable parameters
sNewTuneParms = sprintf('%s,%s',sTuneParms, tune_name);

sStorageClass = 'Auto';
sTypeQualifier='';

for n=1:length(sNewTuneParms)
if (sNewTuneParms(n) == ',')
sStorageClass = sprintf('%s,Auto',sStorageClass);
sTypeQualifier = sprintf('%s,',sTypeQualifier);

end
end

set_param(bdroot,'TunableVars', sNewTuneParms);
set_param(bdroot,'TunableVarsStorageClass', sStorageClass);
set_param(bdroot,'TunableVarsTypeQualifier', sTypeQualifier);

% Remove the tunable parameter from the system tunable parameter
table
sTuneParms = get_param(bdroot, 'TunableVars');
n = findstr(sTuneParms, sParameterToRemove);
if n > 0

s1 = '';

for i=n:length(sTuneParms)
if sTuneParms(i) == ','
s1 = sTuneParms(1:n-1);
s2 = sTuneParms(i:length(sTuneParms));
sNewTuneParms = [s1 s2];
break;
end
end

% if no comma found then just truncate
if length(s1) == 0

if (n > 1)
if (n > 2)
if sTuneParms(n-1) == ','
sNewTuneParms = sTuneParms(1:n-2);
else
sNewTuneParms = sTuneParms(1:n-1);
end
else
sNewTuneParms = sTuneParms(1:n-1);
end
else
sNewTuneParms = '';
end

end
end

sStorageClass = 'Auto';
sTypeQualifier='';

for n=1:length(sNewTuneParms)
if (sNewTuneParms(n) == ',')
sStorageClass = sprintf('%s,Auto',sStorageClass);
sTypeQualifier = sprintf('%s,',sTypeQualifier);

end
end

set_param(bdroot,'TunableVars', sNewTuneParms);
set_param(bdroot,'TunableVarsStorageClass', sStorageClass);
set_param(bdroot,'TunableVarsTypeQualifier', sTypeQualifier);
Post by Chris Perry
Sorry Phil it didn't work for me.
tpTest = 1
set_param(gcs,'TunableVars','tpTest')
I get an error message - SubSystem block does not have a parameter
named 'TunableVars'
Also, once I add a parameter to the tunable parameters table, how do
I programmatically remove it?
Thanks,
-Chris
Post by Phil Goddard
There are a couple of ways to tackle this.
A. If you are using straight MATLAB variables for your
parameters,
Post by Phil Goddard
e.g. you say K1 = MyValue to create the parameters, then you
need
Post by Chris Perry
Post by Phil Goddard
to
set the following 2 model properties.
set_param(gcs,'TunableVars','K1,K2,K3,K4');
set_param(gcs,'TunableVarsStorageClass','Auto,Auto,Auto,Auto');
If you go to the inline/tunable parameters dialog you'll see
that
Post by Chris Perry
Post by Phil Goddard
the
parameters now appear just if you'd gone to that dialog to
define
Post by Chris Perry
Post by Phil Goddard
them as tunable.
Have a look at the doc for set_param if you're not familiar
with
Post by Chris Perry
Post by Phil Goddard
that
function.
B. Alternatively, if you are using Simulink.Parameter objects
to
Post by Chris Perry
Post by Phil Goddard
create the variables then you need to set their
RTWInfo.StorageClass
property to be anything other than auto.
Have a look in the doc for an explanation of how to do this,
and
Post by Chris Perry
Post by Phil Goddard
what
to set the parameter to.
Phil.
Post by Christopher Perry
In Simulink, if you set the inline option in the
Configuration/Optimization for your model, you can make a
parameter
Post by Christopher Perry
tunable by hitting the Configure button and adding it to
the
Post by Chris Perry
Post by Phil Goddard
Global
Post by Christopher Perry
(tunable) parameters list.
I want to perform the same functionality programmatically
yet I have been unable to find a Matlab command to do this?
Can this be done programmatically?
Phil Goddard
2007-04-04 14:49:39 UTC
Permalink
Are you sure that you used gcs (i.e. the current system/model) and
not gcb (the current block)?
I'm not sure why gcs whould give an error message about a SubSystem
block, while gcb would do so if the current block is a subsystem.

Try
Post by Chris Perry
gcs
and see if the name of your model is returned.

Also try
Post by Chris Perry
get_param(gcs,'ObjectParameters')
and have a look for a property related to tunable variables.

Failing that, get on to tech-support at The MathWorks, they'll be
able to tell you exactly what needs to be done for your version of
MATLAB.

Phil.
Post by Chris Perry
Sorry Phil it didn't work for me.
tpTest = 1
set_param(gcs,'TunableVars','tpTest')
I get an error message - SubSystem block does not have a parameter
named 'TunableVars'
Also, once I add a parameter to the tunable parameters table, how do
I programmatically remove it?
Thanks,
-Chris
Loading...