MATLAB: How to create a custom mask dialog for the Simulink block that interacts with the MATLAB base workspace variables

assigninbasedialogevalinevaluatelayoutmaskoutparameterreadsimulinkvariablevariablesworkspacewrite

I would like to create a mask dialog where I can enter a variable name in one field, and have another field update with that variable's current value according to my base workspace. I would also like to be able to edit the value associated with the variable in the mask dialog, and have it change the actual value of the variable in the base workspace.
Is this behavior possible with Simulink mask dialogs? This would save me time from switching back and forth between my Simulink model and base MATLAB workspace when adjusting variables.

Best Answer

Yes, this behavior is possible. See the attached example, "read_write_mask_value.slx".
First, the proper mask fields needs to be created in the "Parameters & Dialog" tab of the Mask Editor. For each variable, you will need two parameters - one for the variable name, and one for the variable value. The attached example demonstrates this for two variables: Gain, and Delay.
To pull the value from the variable stored in the workspace, write a mask callback for the parameter that holds the variable name (not the variable value). To access the callback code for a parameter, click the pencil in the "Dialog" section of the "Property Editor" in the "Parameters & Dialog" tab of the Mask Editor. In the attached example, this callback code is in "g1_var" and "d1_var".  The callback code evalutes the variable name entered in the "g1_var" mask parameter, and sets the "g1_val" mask parameter to that value.
To apply a changed value back to the associated variable in the base MATLAB workspace, write a mask callback for the parameter that holds the variable value (not the variable name). In the attached example, this callback code is in "g1_val" and "d1_val". The callback code reads the value entered in the "g1_val", and assigns it to the variable in the base workspace with the name matching "g1_var".