Different simulation behavior if simulation is called from within function vs. from script
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steve Miller
2021-11-6
I think we would need to see the model in order to diagnose the problem.
--Steve
回答(1 个)
Steve Miller
2021-11-17
I believe the 'SrcWorkspace','current' is different if you call from within a function or a script.
See this answer.
--Steve
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!