How do I set user-defined parameters in an Embedded MATLAB Function Block from the MATLAB command prompt in Simulink 6.0 (R14) and later versions?
5 views (last 30 days)
Show older comments
MathWorks Support Team
on 18 Jan 2013
Edited: MathWorks Support Team
on 6 Oct 2020
I have created a model with an Embedded MATLAB Function Block and would like add user-defined parameters for the block using the command line API.
Accepted Answer
MathWorks Support Team
on 18 Jan 2013
To create an Embedded MATLAB Function Block from the command line, perform the following steps:
1. Use the FIND method to find the Embedded MATLAB function block (Stateflow.EMChart object) with the right name in any open models. If the block name is unique, execute the following command at the MATLAB prompt:
B = S.find('Name','myBlockName','-isa','Stateflow.EMChart');
If the block name is not unique, it is still possible to find the object using the FIND methos. For more information on this methos execute the following command at the MATLAB prompt:
web([docroot,'/toolbox/stateflow/api/find.html'])
2. Add extra inputs to the Embedded MATLAB Function block. To do this execute the following commands at the MATLAB prompt:
line1='function y = fcn(u,param1,param2)';
line2=' y = u*param1*param2;';
B.Script = line1;
B.Script = line2;
3. By default, those parameters will be Inputs, and you will have to change their scope to Parameter. To do that, get the handle to the inputs with the command:
ins = B.inputs;
4. "Ins" will be an array of structs, with the first one being the port input "u", and the rest will be your added inputs (in this case "param1" and "param2").
5. Set those added inputs' scopes to Parameter with the command:
set(ins(2),'Scope','Parameter');
set(ins(3),'Scope','Parameter');
NOTE: For newer versions (~R2012b), name of the following terms have changed :
a. Embedded MATLAB Function Block - is now called 'MATLAB Function Block'
b. Stateflow.EMChart - is now called 'Stateflow.Chart'
1 Comment
Walter Roberson
on 30 Nov 2015
Edited: MathWorks Support Team
on 6 Oct 2020
Embedded MATLAB Function Block was renamed in R2011a
I do not find the change to Stateflow.EmChart documented in the release notes so I do not know which version it occurred in.
More Answers (0)
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!