How can I set the source workspace to "caller" when using a the SIM command from a function in Simulink 7.4 (R2009b)?

In previous releases I was using the following command to specify to the SIM command to use the "current" workspace instead of the "base" workspace when called from inside a function:
[t,x,y] = sim(bdroot,simset('srcworkspace','current'))
In R2009b, the SIMSET function has been deprecated.

 Accepted Answer

This backward incompatibility in Simulink 7.4 (R2009b) was necessary for the SIM command to be compatible with the parallel computing command PARFOR.
In order to work around this issue, it is recommended to assign the required variables to the model workspace. For example, inside the function use:
hws = get_param(model, 'modelworkspace');
% Here it is possible to define as many parameters as possible
a = 5;
list = whos; % Get the list of variables defined in the function
N = length(list);
% Assign everything in the model workspace
for i = 1:N
hws.assignin(list(i).name,eval(list(i).name));
end
See attached files for a complete example.
If you are not planning to use the SIM command inside a PARFOR loop, it is possible to pass a "SrcWorkspace" parameter to the SIM command, for example:
simOut = sim( mymodel, 'SrcWorkspace', 'current')

More Answers (0)

Categories

Products

Release

R2009b

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!