MATLAB: How to access variables in the model workspace from model callback functions

simulink

How can I access variables in the model workspace from model callback functions?

Best Answer

Model callbacks such as "InitFcn" can by default, only access the base workspace. In order to access model workspace variables, you need to do something like (within the "InitFcn" for example):
% Extract the model workspace obj
wksp = get_param('Model', 'ModelWorkspace');
% Extract the relevant model workspace variable
newVar = getVariable(wksp,'modelWkspVar');