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

51 views (last 30 days)
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

MathWorks Support Team
MathWorks Support Team on 19 Aug 2011
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')
  1 Comment
Eeshan Mitra
Eeshan Mitra on 23 Apr 2018
Edited: MathWorks Support Team on 23 Feb 2022
Hi rotton
Without knowing what release you are attempting to upgrade your script to, it might be difficult to identify why the error occurs.
Please try out the following example which contains an instance of setting 'SrcWorkspace' to 'current':
If the example runs fine but issues persist with your code, please put in a Technical Support request so that one of our engineers can investigate further.
Regards Eeshan

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2009b

Community Treasure Hunt

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

Start Hunting!