Clear Filters
Clear Filters

Question on Simulink Callback Scope

7 views (last 30 days)
Peter O
Peter O on 17 Sep 2012
Hello!
I'm trying to perform a calculation based on some simulation parameters in Simulink before the simulation runs but after it's loaded. Basically, I need some constraints evaluated at run-time and I'd rather not have them eval'd inside the block diagram.
For compactness and portability I'd like to keep the constraints as simulink parameters defined in the Model Workspace, and I'm running into a few problems understanding the scope of the callback functions.
At the moment I have the following (simplified) code sitting in the InitFcn:
%Map Handles
hws = get_param(bdroot,'modelworkspace');
SPEED_MIN = evalin(hws,'SPEED_MIN');
FLOW_RATIO_MIN = evalin(hws,'RATIO_MIN');
V_MIN = evalin(hws,'V_MIN');
FLYWHEEL_R = evalin(hws,'FLYWHEEL_R');
minspeed_rad = SPEED_MIN.Value*(2*pi/60);
w_min = minspeed_rad * RATIO_MIN.Value;
V_MIN.Value = 1/2 * minspeed_rad * FLYWHEEL_R.Value;
My questions:
  1. Is this the "right" way to do it? If it's one of the right ways, what are the other ones? I'm not super comfortable with the idea of using "evalin" to grab handles to the workspace vars. Also, it's leaving all of the junk in the MATLAB base workspace when it's through, so it's clearly not evaluating in the scope I would like (model-level). Is there a way to import an M-File to the model file for evaluation like you can import MAT-files to the model workspace?
  2. Why does pasting the above code in the StartFcn callback cause Simulink to throw an "Attempted to Access Read only workspace" error while the InitFcn runs fine?
Thanks, Peter

Answers (1)

Guy Rouleau
Guy Rouleau on 17 Sep 2012
Interesting...
1. The model workspace can be a MAT-file, but also a MATLAB file. Based on your description, this seems like what you are looking for. This is covered here:
You should be able to execute your code in this MATLAB file and get ride of the evalin.
2. This is expected. It is not possible to write to the model workspace once the initialization is completed. This is by design.
InitFcn executes before model initialization, and startFcn executes between the end of model initialization and time step 0 of your simulation. This is why you get the error.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!