Initializing parameters from an mfile function to a simulink file

6 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by