Initializing parameters from an mfile function to a simulink file

5 ビュー (過去 30 日間)
Sina Hashemi
Sina Hashemi 2021 年 10 月 4 日
回答済み: Paul 2021 年 10 月 5 日
Hello everyone,does everyone know how I can solve the following problem? I have several mfiles and one simulink file slx. The simulink file is run by sim command located in a sub-function. Since the variable into an mfile changes and then calculations performed, the output of this sub-function is fed to the simulink as its parameters. At this moment, an error is returned indicating undefined parameters in simulink. This is initially because data is not available in workspace for running simulink. So, how can I import data of an mfile function to a simulink file? Thanks
  1 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 10 月 4 日
hello
in your m file , you have to do all variables declaration / initialisaton before you do sim() with your simulink model.

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

回答 (1 件)

Paul
Paul 2021 年 10 月 5 日
If I understand your workflow correclty, it looks something like this:
function simout = myfunction(inputarg)
simparam1 = 5*inputarg; % compute the simulation parameter based on the input
simout = runsim(simparam1);
end
function simout = runsim(simparam1)
simout = sim('thesimulation')
end
That code won't work because, as you saw, the block parameter that you want to take the value of simparam1 is not defined. Instead you can use a SimulationInput object. That link shows how with examples. Something like this, at least to get started:
function simout = myfunction(inputarg)
simparam1 = 5*inputarg; % compute the simulation parameter based on the input
simout = runsim(simparam1);
end
function simout = runsim(simparam1)
in = Simulink.SimulationInput('thesimulation'); % create the object
in = in.setVariable('thegain',simparam1); % assumes a block parameter called thegain
simout = sim(in);
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by