Running model callbacks in model workspace

Noticed that model callbacks such as PostLoadFcn, StartFcn etc run in base workspace, and they cannot access variables defined in model workspace unless they are evalin'ed in model workspace. Is there a way to change this behaviour and tell the model to evaluate the expressions of its model callbacks in the model workspace instead?

답변 (1개)

Ryan G
Ryan G 2013년 5월 13일

0 개 추천

I don't believe you can change the default behavior here. How much extra work is using the evalin function? Are you calling a lot of functions in the callbacks?

댓글 수: 5

Rey
Rey 2013년 5월 13일
편집: Rey 2013년 5월 13일
Well, it's not a a lot of extra work, but I don't understand why it was designed to behave so. Why doesn't workspace hierarchy apply to model callbacks too? I was trying to pack a single model file with everything it needed, without cluttering the base workspace and needing external scripts.
One more thing I don't understand is why using evalin to learn the value of a variable causes the model to become "dirty". I can rather use model_ws_object.getVariable(), but I haven't been able to verify the back-compatilibity of that.
Ryan G
Ryan G 2013년 5월 13일
I think the complication may stem from the fact it's calling the MATLAB interpreter to perform the callback. When looking at the callback, I would think of it as a way of saying, when this event occurs (PostLoad), send these commands to the MATLAB command window. It may not be ideal in this instance, but that's how it's behaving.
If you are loading data, you can save that in the model workspace and avoid a callback.
If you are using evalin to load something in the model workspace then you are creating a new variable as part of the model and hence dirtying the model. I may not understand what you are doing here though.
Rey
Rey 2013년 5월 13일
편집: Rey 2013년 5월 13일
Ryan, thanks a lot for the information. I guess I understand both points now.
It is likely that I may not be working towards an optimal solution. Nevertheless, my implementation seems to work fine. I am using model callbacks to sync the simulation with the buttons and axes on a GUI, through handles stored as appdata.
Apart from those handles, I already save model related data in its workspace.
I wanted to use evalin to fetch the value of some data, which already exists in the model workspace, and whose name I know. I used to think of the model workspace only as a register of variables, but now I start to see that it is rather "dynamic", "in" which you do operations just like in Matlab command window. Or I am lost again, and would appreciate further clarification.
Rey
Rey 2013년 5월 13일
편집: Rey 2013년 5월 14일
If you have information about since when the workspace_object.getVariable() method is supported, please let me know. I can't seem to find it in the documentation of releases prior to R2012a...
And a final question: Why is it not permitted to define buses in model workspace? Interestingly, Simulink allows evalin'ing scripts to do bus definitions in model workspace, but then it throws an error during compilation. And the error sounds weird: "Bus objects should only be created in base workspace." Not like " can only be created"...
Ryan G
Ryan G 2013년 5월 14일
Not sure about getvariable. But someone already asked about the bus object problem.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Model, Block, and Port Callbacks에 대해 자세히 알아보기

질문:

Rey
2013년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by