How can I load the control variables saved in a .mat file into the workspace without updating the Simulink model?
Show older comments
I have several control variables stored in a .mat file (Variant Configuration) that I would like to load into the workspace. When I load the Variant Configuration with "load ('Model_Configuration')" only the variant container is loaded into the workspace, not the included control variables.
When I update the model, the control variables are loaded into the workspace, but I would like to avoid that.
Is there a way to load the variable from the Variant Configuration directly into the workspace?
Answers (1)
Samayochita
on 13 Aug 2025
Hi Dominik Kurzmann,
I understand that you are trying to load the control variables from a Variant Configuration into the MATLAB workspace without updating the Simulink model.
You could push the control variables to your workspace without requiring a model update by following the steps mentioned below:
- On the Modeling tab, open the Design section and click Variant Manager.
- In the Configurations tab, scroll down to see the Control Variables section. The section shows the name of the currently selected variant configuration.
- Click on the “Export control variables to workspace(s)” button.
You could also achieve the same programmatically by following the steps given below:
- Get the variant configuration data object
varconfigdata = Simulink.VariantManager.getConfigurationData(modelname);
2. Retrieve a specific configuration
cfg = getConfiguration(varconfigdata, "LinInterExpNoNoise");
3. Check if ControlVariables exists, if yes extract control variables else throw an error.
if isstruct(cfg) && isfield(cfg, 'ControlVariables')
vars = cfg.ControlVariables;
Assign control variables to base workspace
for k = 1:numel(vars)
assignin('base', vars(k).Name, vars(k).Value);
end
Links to the relevant documentation:
- https://www.mathworks.com/help/simulink/slref/simulink.variantconfigurationdata.getconfiguration.html
- https://www.mathworks.com/help/simulink/slref/simulink.variantmanager.getconfigurationdata.html
- https://www.mathworks.com/help/simulink/var/create-activate-variant-configurations.html#:~:text=To%20export%20the%20control%20variables%20to%20the%20data%20sources%20where%20they%20are%20stored%2C%20click%20the%20Export%20control%20variables%20to%20workspace(s)%20button
- https://www.mathworks.com/help/matlab/ref/assignin.html
Hope this helps.
Categories
Find more on Component-Based Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!