Can I create a seperate workspace for Simulink Callback Variables? (MATLAB 2016b)
Show older comments
In my models, I use a lot of dynamic masking to initialize parameters, which involves lots of long Callback functions. Also, creating/editing code in the Callback Editor seems to be very unpractical when the code is over a dozen lines long. A simple example of what I mean is as follows:
gear = {'1' '2' '3' '4' '5' 'R'};
angles = [0 10 20 25 30 -11];
str = get_param(gcb,'Position_final');
i = strcmp(str, Gear);
set_param(gcb,'Position_final_fine',angles(i));
This reads the value selected from a popup box, and changes the value of a spinbox so that angles between the selected value can also be used. This length code is manageable, but some of my Callback functions are over 400 lines long. As well as this, I will have now saved the parameters gear, angles, str and i in my workspace. Which with longer parts of code, turn into 20-30 parameters will quickly fill up my workspace. I could use clear, to clear the entire workspace (possibly damaging other parts of my model that rely on the workspace), or clear the parameters I have used, but then they would still overwrite parameters I have used (considering I use a lot of common acronyms and letters repeatedly).
One solution would be to create a function, and then have the Callback call the external function, which would increase readability of the code, and allow me to use multiple functions for similar Callbacks. However, then I will need to carry around many extra functions with the model, which I would rather avoid.
Is it possible to either,
1) create a separate workspace for the Callback function, in which it is not connected to the global workspace.
or
2) create a function that the Callback can access, and embed this in the Simulink .slx file, so that I do not need to carry round extra functions with the program.
Best regard,
Mathew
Accepted Answer
More Answers (1)
Mark Schwab
on 25 Jan 2018
0 votes
Based on the description of your workflow, I would recommend using the model workspace. Every Simulink model has a workspace of its own. For this workspace, you can think of the model as a function. The variables in this workspace are only visible in the scope of the model. I have included a link to documentation on the model workspace below:
I would recommend exploring this documentation and other documentation on the model workspace to see if it fits your use case.
1 Comment
Mathew Thorne
on 20 Feb 2018
Categories
Find more on Simulink Environment Customization 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!