Running Simulink from within a Matlab function
1 次查看(过去 30 天)
显示 更早的评论
I was running Simulink from the main script with FastRestart and avoiding the use of simset and all was good.
However, after enclosing the script in a function so that I can call it from a different script for different input data, I now get an error related to the workspace. Basically, Simulink is no longer able to access the workspace within the function.
I have found solutions on-line using simset, but this is not an option in my case. Assuming sfile is my Simulink file, I first load the system
load_system(sfile);
and then set-up the FastRestart settings
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
Now, the error occurs on this last line, i.e. when I am updating the Simulink file. Basically, Simulink returns an error because it does not recognize mdl.tStep, i.e. the step size length, as a structure because it is not specified. In fact, it is because it is in the workspace.
So, my question is how I can sort out this problem. Is it possible to use set_param to change the workspace now that simset is deprecated? Or should I rather set all variables through set_param individually?
0 个评论
回答(1 个)
Brandon Eidson
2017-3-15
Hey Enrico, the reason you are getting an error now that you have enclosed the script in a function is because a function has its own workspace separate from the base workspace. You can read more about this distinction in the documentation linked to below.
Because you call the model from the function, the model only has access to the function workspace. If the simulation depends on any user-defined variables, such as your "tStep", and you want to update or simulation the model from a function, you will need to pass those user-defined variables to the function as arguments.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!