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

1 次查看(过去 30 天)
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.
Suggestions?
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.

回答(1 个)

Sujit Muduli
Sujit Muduli 2018-3-6
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.
  2 个评论
Jason Nicholson
Jason Nicholson 2018-3-6
编辑:Jason Nicholson 2018-3-6
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 2018-5-2
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by