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

조회 수: 1 (최근 30일)
Jason Nicholson
Jason Nicholson 2018년 3월 3일
댓글: Jason Nicholson 2018년 5월 2일
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