Use a .m file to import variables in to a Simulink Matlab Function box
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 26 Juin 2012
Please do not name your own script as "input.m" as that will cause problems with the MATLAB function named input()
govindan
le 1 Mar 2016
Réponse acceptée
Plus de réponses (2)
Walter Roberson
le 26 Juin 2012
1 vote
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 commentaires
Walter Roberson
le 26 Juin 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
le 26 Juin 2012
Hasnain Lanewala
le 26 Juin 2012
0 votes
Catégories
En savoir plus sur Interactive Model Editing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!