Different simulation behavior if simulation is called from within function vs. from script
1 view (last 30 days)
Show older comments
Hello,
I have a simulink model which uses a variety of parameters from the workspace. If I execute the exact same piece of code (initialization of parameters+ run simulation) from within a function vs. from a script the simulation results are (slightly) different which leads to the simulation running into a stopping condition if called from the script, but finishing smoothly if called from within the function.
The two different pieces of code are like this:
params_val = goodRuns_all(ind_choosen,:);
%% Version 1 from script
parameters = params_val;
init_script;
MP = setParams(MP, 1, parameters(8), parameters(10), parameters(11), parameters(12), parameters(13));
simulation_length = 30;
options = simset('SrcWorkspace','current');
warning('off','all')
simOut = sim('model_name', [],options);
warning('on','all')
%% Version 2 with function call
simOut_func = runSim(params_val);
%% Function declaration
function simOut = runSim(parameters)
init_script;
MP = setParams(MP, 1, parameters(8), parameters(10), parameters(11), parameters(12), parameters(13));
simulation_length = 30;
options = simset('SrcWorkspace','current');
warning('off','all')
simOut = sim('model_name', [],options);
warning('on','all')
end
Also, if I first execute the code from the script and then (without a clear) run the function, also the simulation called from the function runs into a stopping condition whereas if I directly call the function it does not.
What could be the reason for the changed behavior?
1 Comment
Steve Miller
on 6 Nov 2021
I think we would need to see the model in order to diagnose the problem.
--Steve
Answers (1)
Steve Miller
on 17 Nov 2021
I believe the 'SrcWorkspace','current' is different if you call from within a function or a script.
See this answer.
--Steve
See Also
Categories
Find more on General Applications 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!