SIM command clears workspace

I'm trying to run a simulink simulation from an M-file. The problem is that running 'sim' clears the current workspace, and thus clears variables needed in the simulink simulation and also nested for loop variables.
I've tried
simset('SrcWorkspace','current', 'DstWorkspace', 'current');
As a 'SIM' options parameter as suggested in previous posts with similar titles, however, this does absolutely nothing.
How do I stop 'SIM' clearing my workspace? Running MATLAB R2011b.

10 Comments

I thought that SrcWorkspace and DstWorkspace were deprecated. However, the doc page http://www.mathworks.com/help/simulink/slref/sim.html still shows SrcWorkspace and doesn't say it's deprecated. But that page doesn't include DstWorkspace as a valid parameter. Is it no longer supported?
I have the same problem. Matlab 2016a. SIM works ok from the script, however, when you put SIM inside a function it clears the workspace all of the variables except those that are the inputs to this function. I checked the callbacks. It is all clear!!!
hi I have this problem and I have Matlab 2015b. If it is solved please tell me.
Jan
Jan on 30 Sep 2017
Edited: Jan on 30 Sep 2017
@Mohammad: Please use flags only to inform admins and editors about messages, which violate the terms of use e.g. by rudeness. Thanks.
Please explain clearly, what your problem is. Actually sim should not clear the current workspace. Post the relevant part of you code, which reproduces the problem.
Mohammad
Mohammad on 30 Sep 2017
Edited: Mohammad on 30 Sep 2017
Sorry about the flag thing. My bad! This is the code which runs the simulink model which produces two variables q and m. I use simout block but I noticed that q which is a 100*3 matrix (sim time*parameters) and also m is a 100*2 (sim time * params) are produced only for sample times which I set. Notice that I use it in a for loop and I do the simulation with the data produced from the previous step so each time I simulte from e.g. [1 3](time) and I get 2 variables each time. I mean I want [1 3] to go q(T 1) to q(T 3) and [4 6] to go to q(T 4) and q(T 6) and so on. T is simulation time. I hope it is clear. Thanks
clc
clear all
T=100;
q=zeros(T,3);
m=zeros(T,2);
for i=1:T
Q=tf([q(i,1) q(i,2)],[1 q(i,3)]);
M=tf([m(i,1)],[1 m(i,2)]);
options = simset('SrcWorkspace','current');
sim('adp_imc_1',[i i+1],options)
end
Mohammad
Mohammad on 30 Sep 2017
Edited: Mohammad on 30 Sep 2017
I really needed your help . I had to use the flag again. That was the only way to draw your attention . Sorry. Please help me with this problem.
Jan
Jan on 30 Sep 2017
Edited: Jan on 30 Sep 2017
@Mohammad: Stop using flags to catch attraction. I have explained their meaning already, so do not misuse them. What would happen, if everybody sets flags because he needs help? Flags would become useless. Remember, that for all users the own problems are the most important ones. Then trying to push the answering members is really impolite. Even bumping with a comment is not liked.
This is even not your own thread, but an old one. Instead of this "thread-hijacking" prefer to open a new thread for your question.
I do not not understand the description in your comment and cannot see the connection to the original question, which concerns unexpectedly cleared variables. What exactly is the problem?
This is the wrong way to ask for help.
I have a simulink model. It has two blocks that need to be updated. So I try to use the sim command in a for loop. But it seems that it cleans the workspace and there is no data to update those blocks. This is the problem.
What do you think about the suggestion to open a new thread? Then it is possible to post an answer in the section for answers and you can accept an answer, if it solves your problem.
I did open a new thread. But neither you nor anybody else answered me.

Sign in to comment.

Answers (1)

Ryan G
Ryan G on 2 Nov 2012
This SIM command should not clear your workspace, you should not have to workaround that.
Check your model callbacks and see if there is any functions/scripts being called that clear the workspace.

Products

Asked:

on 1 Nov 2012

Commented:

on 2 Oct 2017

Community Treasure Hunt

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

Start Hunting!