Load variables into a simulink model from a Matlab funtion.

13 次查看(过去 30 天)
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

采纳的回答

Paul
Paul 2021-5-1
编辑:Paul 2021-5-1
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 个评论
Paul
Paul 2021-5-2
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Troubleshooting 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by