Use a .m file to import variables in to a Simulink Matlab Function box
216 views (last 30 days)
Show older comments
I have a simulink model that consists of several MATLAB function blocks, each with inputs and outputs from neighboring MATLAB function blocks. Now, each of these MATLAB function blocks require certain variables to have predefined values, which I define in a separate .m script file. The problem, of course, is that when I use these variables to calculate other things of interest in my MATLAB function block, it gives me an error since the MATLAB function block doesn't know what those predefined values are. I have imported my "Input".m script file in the model workspace and it shows all the variables/objects in the contents panel. My question is how can I use these variables/objects (that can be seen in the model workspace) in my MATLAB Function block. I have looked at Simulink.Signal help on mathwork's website and it hasn't helped me much. For clarity I have copied the code from one of the MATLAB function blocks below. I have also written the problem in curly brackets in the code:
function [ NOx_R1, R1_stuff_out ] = fcn(mass_flows)
RN = 1;
mF_R1 = mass_flows(1);
mA_R1 = mass_flows(2);
f_R1 = mF_R1/mA_R1; {f_R1 has a value defined in the .m script file}
PIRP = [mA_R1, Air(1), Air(2)]; {Again, array 'Air' is defined in .m file}
[PORP CAP DAP] = ReactorInletProperties(Air);
Tin_R1 = ReactorTin(RN, PIRP, PORP, CAP, DAP);
phi_R1 = f_R1/f_stoich;
[Cp_R1 Tc_R1] = Tc_calculator(Cp_data, Tc_data, phi_R1, Tin_R1); {Cp_data is defined in .m file}
NOx_R1 = PrimaryZoneNOx(phi_R1,P3,Tc_R1,tres1); {p3 and tres1 are defined in .m file as well}
R1_stuff_out = [mF_R1, mA_R1, mF_R3_R1, mA_R3_R1, Tc_R1, Cp_R1];
Any help will be greatly appreciated!! Thanks in advance
Hasnain
2 Comments
Walter Roberson
on 26 Jun 2012
Please do not name your own script as "input.m" as that will cause problems with the MATLAB function named input()
govindan
on 1 Mar 2016
Accepted Answer
TAB
on 26 Jun 2012
If you have variables already defined in Model workspace or Matlab's base workspace you can accees (read) them in the Embedded Matlab function block as Parameter.
- Open Embedded Matlab editor. Goto Tools-->Edit Data/Ports.
- Click on Add Data button to add new data.
- Set the name of this Data variable same as the variable you have in workspace.
- Set the scope of this variable as Parameter.
Once defined as the parameter, this variable will be resolved from your model workspace (or base workspace). Note that, model can only read the value of parameters, but can't modify them,
2 Comments
More Answers (2)
Walter Roberson
on 26 Jun 2012
I am not experienced at all with Simulink, but based upon conversations that have gone on in the past, I would think that you should save the variables into a MATLAB workspace and use a From Workspace block. Or save them in a .mat file and use a From File block.
2 Comments
Walter Roberson
on 26 Jun 2012
Having all of the variables in your model workspace available in your MATLAB function blocks is not going to happen: it violates the principle of variable hiding that "workspaces" were implemented for.
You could declare all of your variables to be global, and then global them in each function block that needs them. This is, as you might imagine, not very pretty. And it is prone to problems where one of the function blocks has a logic error that ends up changing the global variables.
The way to get each variable visible without violating information hiding is to declare each of the variables as its own function (in its own .m) that returns the appropriate constant value. Then as long as those functions are on the path, they would be visible. An example of this is pi, which is implemented as a function rather than as a constant known to the parser.
See Also
Categories
Find more on Naming Conventions 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!