Initializing parameters from an mfile function to a simulink file
8 views (last 30 days)
Show older comments
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 Comment
Mathieu NOE
on 4 Oct 2021
hello
in your m file , you have to do all variables declaration / initialisaton before you do sim() with your simulink model.
Answers (1)
Paul
on 5 Oct 2021
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
0 Comments
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!