Load variables into a simulink model from a Matlab funtion.

조회 수: 22 (최근 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에서 Data Logging에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by