MATLAB Answers

Is there a clever way to isolate Simulink Parameter callback workspace?

1 view (last 30 days)
Jason Nicholson
Jason Nicholson on 3 Mar 2018
Commented: Jason Nicholson on 2 May 2018
The run command runs a script in the caller workspace. That got me thinking that there might be a clever way to run code in Simulink Parameter callback in an isolated workspace. I don't like the fact that the Parameter callbacks pollutes the base workspace. Variable collision(s) is unlikely but it is messy when callbacks are complicated.
EDIT: One file is preferred. Right now I am restricted to use a single Simulink file otherwise I would just use a MATLAB function for the functionality I am looking for.


Sign in to comment.

Answers (1)

Sujit Muduli
Sujit Muduli on 6 Mar 2018
Hi Jason, You may want to use Model Workspace or a Data dictionary if you don't want to pollute your base workspace. However, there is one alternative approach. You may create a MATLAB Function to perform all the calculations and since each MATLAB function has its own workspace it won't pollute your base workspace. You may also find the links below useful to do operations in different workspaces.


Jason Nicholson
Jason Nicholson on 6 Mar 2018
How do you get a Callbacks to execute in the model workspace? I don't think the model workspace or the data dictionary are relevant.
evalin only allows executing of code in base or caller. The parameter callback is already executing in the base workspace. I don't think this helps.
Calling a matlab function is appropriate because it's workspace is isolated. However, one of my requirements I forgot to add is I am restricted to "one file." Currently, my company has us developing models that are "one file" which causes us to keep everything in a single Simulink file. I actually think this approach may be less than ideal and restrictive to what is best practice. I like the idea of using a matlab function and am using this as I develop my model currently. It works well.
I do have another idea. It involves an anonymous function call to a hidden parameter callback. It will work but I think it is ugly code because it is harder follow the code execution path.
Jason Nicholson
Jason Nicholson on 2 May 2018
You never answered my question about getting variables assigned to the model workspace. Here is the answer to that question:
modelWorkspace = get_param(gcs, 'ModelWorkspace');
modelWorkspace.assignin('myvar', 15);
The previous bit of code gets the modelWorkspace from the current Simulink model. It then assigns a variable myvar the value 15 in the model workspace. You can use the Model Explorer to check this.
Since you have a way to access the model workspace now. It may be possible to use evalin although I haven't tested this.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!