Load variables into a simulink model from a Matlab funtion.

32 ビュー (過去 30 日間)
Carlos Jiménez Guerra
Carlos Jiménez Guerra 2021 年 5 月 1 日
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.
Carlos Jiménez Guerra
Carlos Jiménez Guerra 2021 年 5 月 2 日
That works! Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTroubleshooting についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by