Initializing parameters from an mfile function to a simulink file

조회 수: 9 (최근 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 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