Load variables into a simulink model from a Matlab funtion.
13 views (last 30 days)
Show older comments
Carlos Jiménez Guerra
on 1 May 2021
Commented: Carlos Jiménez Guerra
on 2 May 2021
Hello,
I am working in a project which needs to execute a Simulink model from a MATLAB Function. My problem is when I try to convert a standard script to a MATLAB function. In the places of the parameters, I put the same variable names in Simulink and in the MATLAB function... So, when I execute the function, in the workspace must be a variable with the name and value... but I get this message:
I am looking the way to save the parameters of the function as base workspace variables to be load in the model... My idea is the like this:
%As example (I'm working with simscape) in the parameter of a resistor I put R1 like the picture above:
function [out] = myFunction(R1)
%start of the function
load_system('myCircuit');
find_system('Name','myCircuit');
%Run simulink model
sim('myCircuit.slx');
%dataOut
out = ans.A;
%end of the function
I tried the "setparam()" before the simulation to, without any result...
Thank you for reading,
Carlos
0 Comments
Accepted Answer
Paul
on 1 May 2021
Edited: Paul
on 1 May 2021
Check out the doc page for the sim() command
doc sim
The sim() command has some options that do what you want, including the SrcWorkspace Name/value paraemeter and the SimIn object.
Also, consider using the output of the sim() command to keep eveyrthing nice and tidy after the simulation completes
out = sim('myCircuit', ...)
3 Comments
Paul
on 2 May 2021
I assume that R1, Res1, etc. are block parameters in your model. In this case, make sure these variables have valued inside myFunction prior to the call to sim(). Then the sim command would look like:
simOut = sim('myCircuit','SrcWorkspace','current')
However, there are some caveats with this approach as discussed on the doc page for sim(), which might or might not apply to your model.
More Answers (0)
See Also
Categories
Find more on Troubleshooting in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!