Use a .m file to import variables in to a Simulink Matlab Function box

216 views (last 30 days)
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

Accepted Answer

TAB
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.
  1. Open Embedded Matlab editor. Goto Tools-->Edit Data/Ports.
  2. Click on Add Data button to add new data.
  3. Set the name of this Data variable same as the variable you have in workspace.
  4. 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,
Also, if possible, in the workspace defined your variables as Simulink.Parameter.

More Answers (2)

Walter Roberson
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
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.
Hasnain Lanewala
Hasnain Lanewala on 26 Jun 2012
Thanks very much for you reply! Actually, this is what I was going to do for all the MATLAB function blocks until I found out, through TAB, how to use variables in the workspace as parameters in to the MATLAB function block.

Sign in to comment.


Hasnain Lanewala
Hasnain Lanewala on 26 Jun 2012
Adding a From Workspace block would involve a block in the model diagram and depending on the number of my MATLAB function blocks I will have to add that many 'From Workspace' blocks. I am really trying to avoid that. Ideally, I would like to do something where ALL the variables in my Model Workspace (imported from the matlab script file) could be detected by my MATLAB function blocks.
Also, the name of my input file isn't actually 'Input', its different. Sorry for the confusion.

Products

Community Treasure Hunt

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

Start Hunting!