http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533
MATLAB Central Newsreader  executable simulink with tunable transfer function
Feed for thread: executable simulink with tunable transfer function
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 20 Jul 2009 11:59:03 +0000
executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#666595
Georg
Hi everyone,<br>
<br>
my simulink model exists of:<br>
<br>
ConstGainTransfer_FcnTo_File<br>
<br>
generating the executable:<br>
<br>
set_param('my_model ','RTWInlineParameters','on');<br>
set_param('my_model ','TunableVars','K,den,num');<br>
set_param('my_model ','TunableVarsStorageClass','Auto,Auto,Auto');<br>
set_param('my_model ','TunableVarsTypeQualifier',' , , ');<br>
<br>
rtwbuild('my_model ');<br>
param_struct = rsimgetrtp('my_model ','AddTunableParamInfo','on');<br>
save params.mat param_struct<br>
<br>
Now my problem is when I execute the file it is not possible to change the num/den when the length is not the same as in my generated .exe<br>
<br>
<br>
load params.mat;<br>
param_struct = rsimsetrtpparam(param_struct,'K',K,'den',1,'num',[2 1]);<br>
save params.mat param_struct<br>
system('my_model p params.mat')<br>
<br>
??? Error using ==> setOneRTPParam at 153<br>
The variable val has the wrong number of elements to set parameter num.<br>
<br>
My second consideration was to set the models workspace to a matfile<br>
<br>
hws = get_param(mdlName,'modelworkspace'); <br>
hws.DataSource = 'MATFile'; <br>
hws.FileName = 'params'; <br>
hws.saveToSource;<br>
rtwbuild(mdlName);<br>
<br>
But when I change something before starting the executable: <br>
<br>
hws = get_param('my_model ','modelworkspace') <br>
hws.assignin('K', K); <br>
hws.saveToSource;<br>
system('my_model') <br>
<br>
the changes were not applied<br>
<br>
anybody a clue?<br>
/georg

Mon, 20 Jul 2009 17:01:03 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#666714
Phil Goddard
There's a technical and a business reason for not being able to change the length of num or den in the rsim executable.<br>
<br>
technical: the ccode doesn't do malloc or calloc. In the code the appropriate array lengths are assigned and during execution it is checked that the num/den lengths match the expected lengths. If they don't then an error is thrown because the memory allocated and the data being attempted to use don't match.<br>
<br>
business: if you could change the lengths then you create an exe that allowed someone else do what simulink does  allow you to design not just the filter gains but also the actual filter. From a (MathWorks) business perspective that's not a good thing because you could go into competition with them.<br>
<br>
> But when I change something before starting the executable: <br>
> <br>
> hws = get_param('my_model ','modelworkspace') <br>
> hws.assignin('K', K); <br>
> hws.saveToSource;<br>
> system('my_model') <br>
> <br>
> the changes were not applied<br>
<br>
This doesn't work because the exe version is using data in the parameter structure that is part of the ccode (or loaded from a .mat file is you use the p option), not the workspace of the mdl.<br>
<br>
Phil.

Tue, 21 Jul 2009 15:10:21 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#666991
Georg
Thanks for the quick answer Phil!<br>
<br>
I solved the problem by using a (hopefully) long enough num/den.<br>
But know this error occurs<br>
??? Error using ==> rtwgen<br>
Algebraic loops are not supported in generated code.<br>
...should mention that now the transferfunction is feed back<br>
<br>
when I make num/den not tunable there is no error, or I've to put a Memory block in the loop, then I can build the the model. Is this a proper solution?<br>
<br>
anyway, when I run the executable the outputfile contains just NANs. simulated in Simulink the output file is correct!?<br>
<br>
can you give me a piece of advise?<br>
<br>
/georg

Wed, 22 Jul 2009 14:48:01 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#667389
Phil Goddard
I'm very suprised that the same model, with the same data and settings, gives different results in simulation and with RSIM  in fact I don't believe it.<br>
I also don't believe that making the transfer function nontunable gets rid of an algebraic loop.<br>
For any given TF there is either an algebraic loop or there is not.<br>
<br>
In short, if you are getting different results in simulation to RSIM then you are not doing the same thing in simulation and RSIM.<br>
<br>
There is a section of the doc that deals with algebraic loops and suggests 2 or 3 different ways to eliminate them.<br>
Note that by adding the memory block you are changing the dynamics of your system  this may or may not be appropriate depending on your situation.<br>
<br>
Phil.

Wed, 22 Jul 2009 15:01:55 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#667396
Georg
Hi again!<br>
<br>
to break the problem down to the root:<br>
<br>
a simple simulink model with one transfer function num=1 den=[1 1]<br>
I build the model; execute the model with changed parameter den=[0 1]<br>
than the output file contains just NaN's.<br>
<br>
I suppose, that the zero is not exact zero<br>
how can I avoid this problem??<br>
<br>
appreciate any suggestions

Wed, 22 Jul 2009 15:37:02 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#667405
Phil Goddard
<br>
In simulation, the transfer function block checks (amongst other things) that the TF is causal and also strips out leading zeros from the num and den.<br>
(You can verify that by putting in different num's and den's and seeing what the dialog block allows and how the entered values get displayed on the block icon.)<br>
<br>
When you code generate, RTW takes the num and den (which must be allowable values or the model won't build) and creates a structure of the appropriate size to store their values.<br>
The algorithm that it uses in the ccode that represents the TF block will use all elements of the structure (it doesn't know, and can't know automatically, that some of the leading elements are zeros that they should be ignored).<br>
If you put "bad" values into the TF then the code is just going to try to use them.<br>
<br>
The reason you are getting NaN's is because the values are _exactly_ zero.<br>
If you look at the generated yourModelName.c, and look for the MdlDerivatives function, and follow the logic of the code, you'll see that there is a 0/0 in there and hence the result is NaN.<br>
<br>
Essentially your are trying to do something that RTW is not designed for.<br>
<br>
There is no automtic way to get the generated code to recognize that you are trying to do something "bad".<br>
The only thing to do would be for you to go into the code after it has been generated, implement your own MdlDerivatives routine that checks for leading zeros and if they exist ignore them, then use the yourModelName.bat file to do the compilation.<br>
<br>
Phil.

Tue, 28 Jul 2009 15:55:20 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#668821
Georg
"Phil Goddard" <philNOSPAM@goddardconsulting.ca> wrote in message <h47bmu$q8q$1@fred.mathworks.com>...<br>
> <br>
> In simulation, the transfer function block checks (amongst other things) that the TF is causal and also strips out leading zeros from the num and den.<br>
> (You can verify that by putting in different num's and den's and seeing what the dialog block allows and how the entered values get displayed on the block icon.)<br>
> <br>
> When you code generate, RTW takes the num and den (which must be allowable values or the model won't build) and creates a structure of the appropriate size to store their values.<br>
> The algorithm that it uses in the ccode that represents the TF block will use all elements of the structure (it doesn't know, and can't know automatically, that some of the leading elements are zeros that they should be ignored).<br>
> If you put "bad" values into the TF then the code is just going to try to use them.<br>
> <br>
> The reason you are getting NaN's is because the values are _exactly_ zero.<br>
> If you look at the generated yourModelName.c, and look for the MdlDerivatives function, and follow the logic of the code, you'll see that there is a 0/0 in there and hence the result is NaN.<br>
> <br>
> Essentially your are trying to do something that RTW is not designed for.<br>
> <br>
> There is no automtic way to get the generated code to recognize that you are trying to do something "bad".<br>
> The only thing to do would be for you to go into the code after it has been generated, implement your own MdlDerivatives routine that checks for leading zeros and if they exist ignore them, then use the yourModelName.bat file to do the compilation.<br>
> <br>
> Phil.<br>
<br>
Thx Phil!<br>
<br>
Finally I solved the problem by manipulating the ccode. You don't have to implement a new MdlDerivatives but do something in the MdlStart routine<br>
<br>
left shift the den/num x times<br>
x is the number of leading zeros in the denominator<br>
<br>
so th problem is solved with just a few lines of code<br>
<br>
georg

Fri, 30 Nov 2012 22:42:08 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#892797
Murat Kalkan
"Georg" wrote in message <h41m67$7nf$1@fred.mathworks.com>...<br>
> Hi everyone,<br>
> <br>
> my simulink model exists of:<br>
> <br>
> ConstGainTransfer_FcnTo_File<br>
> <br>
> generating the executable:<br>
> <br>
> set_param('my_model ','RTWInlineParameters','on');<br>
> set_param('my_model ','TunableVars','K,den,num');<br>
> set_param('my_model ','TunableVarsStorageClass','Auto,Auto,Auto');<br>
> set_param('my_model ','TunableVarsTypeQualifier',' , , ');<br>
> <br>
> rtwbuild('my_model ');<br>
> param_struct = rsimgetrtp('my_model ','AddTunableParamInfo','on');<br>
> save params.mat param_struct<br>
> <br>
> Now my problem is when I execute the file it is not possible to change the num/den when the length is not the same as in my generated .exe<br>
> <br>
> <br>
> load params.mat;<br>
> param_struct = rsimsetrtpparam(param_struct,'K',K,'den',1,'num',[2 1]);<br>
> save params.mat param_struct<br>
> system('my_model p params.mat')<br>
> <br>
> ??? Error using ==> setOneRTPParam at 153<br>
> The variable val has the wrong number of elements to set parameter num.<br>
> <br>
> My second consideration was to set the models workspace to a matfile<br>
> <br>
> hws = get_param(mdlName,'modelworkspace'); <br>
> hws.DataSource = 'MATFile'; <br>
> hws.FileName = 'params'; <br>
> hws.saveToSource;<br>
> rtwbuild(mdlName);<br>
> <br>
> But when I change something before starting the executable: <br>
> <br>
> hws = get_param('my_model ','modelworkspace') <br>
> hws.assignin('K', K); <br>
> hws.saveToSource;<br>
> system('my_model') <br>
> <br>
> the changes were not applied<br>
> <br>
> anybody a clue?<br>
> /georg<br>
<br>
Hi everyone;<br>
<br>
I tried this code in Matlab R2011a, but it gives an error in system('my_model p params.mat') line. I think the "p" argument is not supported in R2011.<br>
Is there a solution for this problem?

Fri, 30 Nov 2012 23:31:08 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#892799
Phil Goddard
The p option is still supported in R2012b (so it was also in R2011a/b) so that it not the problem.<br>
<br>
Phil.

Mon, 03 Dec 2012 16:55:12 +0000
Re: executable simulink with tunable transfer function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256533#892974
Murat Kalkan
Thank you for your reply;<br>
<br>
I tried again and Matlab throw an error:<br>
Unexpected command line argument: p<br>
<br>
Do you have an idea?<br>
<br>
"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <k9bfjs$q4o$1@newscl01ah.mathworks.com>...<br>
> The p option is still supported in R2012b (so it was also in R2011a/b) so that it not the problem.<br>
> <br>
> Phil.