Initializing parameters from an mfile function to a simulink file

6 visualizzazioni (ultimi 30 giorni)
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 Commento
Mathieu NOE
Mathieu NOE il 4 Ott 2021
hello
in your m file , you have to do all variables declaration / initialisaton before you do sim() with your simulink model.

Accedi per commentare.

Risposte (1)

Paul
Paul il 5 Ott 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

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by