Transfer of Fcn Block between Simulink and MATLAB

Hi, everyone. Before simulation I need to transmit to my Simulink model each time different function which is located in Fcn block. Of course I can pass variables to Fcn block but it is not sufficient. Thanks for your help.

 채택된 답변

Kaustubha Govind
Kaustubha Govind 2011년 10월 17일

2 개 추천

If you are using the Fcn block, you can simply change the setting for the "Expression" before each simulation. For example,
set_param('mymodel/Fcn', 'Expr', 'foo(u(1))')

댓글 수: 2

Thanks. How can I automate this process? Because I have hundreds of Fcn blocks and all of them use the same function.
You can use find_system to find blocks in your model that have BlockType=Fcn (see http://www.mathworks.com/help/toolbox/simulink/slref/find_system.html).
Here is a relevant blog post: http://blogs.mathworks.com/seth/2008/03/04/searching-in-simulink/

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rostyk Spolyak
Rostyk Spolyak 2011년 10월 19일

0 개 추천

Thank you for your answers :)
After executing "find_system" command I get a list with names of all Fcn blocks in my model. So, how can I change the 'Expression' in all blocks? I tried using "set_param" function but It works only with a models. Any idea?

댓글 수: 4

Rostyk: set_param works with models AND blocks. See my answer for how I specify the full path to the block to set the parameter.
I suppose you dont understand my question.
Input argument for function 'set_param' must be the model or block. 'find_system' returns handles or paths to the objects.
I have nearly 50 Fcn blocks. So after executing find_system I get a list of all Fcn blocks. Then I must type 50 commands like this:
set_param('mymodel/Fcn1', 'Expression', 'u*128+5')
set_param('mymodel/Fcn2', 'Expression', 'u*128+5')
...
set_param('mymodel/Fcn50', 'Expression', 'u*128+5')
It is very boring to type everytime 50 commands. So I want to automate this process. I think I can make a script where I will write all this 50 commands and execute it everytime I need. Is there any other possibility? Maybe 'set_param' can receive as an object handle or vector of path to the objects?
Thanks for your help.
set_param cannot handle a cell array of block paths (although get_param can). But it's fairly trivial to loop through the array:
bh = find_system('mymodel', 'BlockType', 'Fcn');
for i = 1:numel(bh), set_param(bh{i}, 'Expression', 'u*128+5'), end
Thanks. I`ll try this.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by