How does the method loadVariablesFromMATFile from Simlink.SimulationInput object work?

19 views (last 30 days)
I have a Simulink model 'input_checkN1P10test1' and want to run parallel computing with various settings of parameters of this model. I want to load the parameters to the model from external .mat file. Those parameters in model are ‘Start Time’ t0, ‘Stop Time’ t1, or even ‘tspan’ show up in Simulink’s ‘If’ Block (see attached), etc.. Short Questions:
1. Is it OK to use method loadVariablesFromMATfile to load values of those parameters from external .mat file?
2. What’s the difference with loadVariablesFromMATFile method other than methords 'setModelParameter' and 'setBlockParameter'?
3. Does loadVariablesFromMATfile load variables from .mat file to model's model work space?
clear all
mdl = 'input_checkN1P10test1'; % mdl has no vars in model ws and data source
N = 10;
tset = linspace(0,1,N+1);
simin(1:N) = Simulink.SimulationInput(mdl);
load_system(mdl)
hws1 = get_param(mdl,'modelworkspace')% should be empty
for i = 1: 10
simin(i) = simin(i).loadVariablesFromMATFile('inputpars_simulink.mat');
simin(i) = simin(i).setVariable('t0',tset(i),'workspace','input_checkN1P10test1');
simin(i) = simin(i).setVariable('upars',50*ones(1,10),'workspace','input_checkN1P10test1');
simin(i) = simin(i).setVariable('vpars', 5*ones(1,10),'workspace','input_checkN1P10test1');
end
simout1 = sim(simin);%

Answers (1)

Satwik
Satwik on 24 Mar 2025
Here are some pointers that aim to answer the short questions. I hope you find them helpful:
1) Yes, using 'loadVariablesFromMATFile' is an appropriate way to load parameters from an external .MAT file for Simulink models. This method allows us to specify a .MAT file containing the variables we want to load into the Simulation input object. It is particularly useful when running multiple simulations with different parameter sets. For more information on this function please refer to the following documentation: https://www.mathworks.com/help/simulink/slref/simulink.simulationinput.loadvariablesfrommatfile.html
2) The 'loadVariablesFromMATFile' method is used to load variables from a specified .MAT file into the Simulation input object. On the other hand, 'setModelParameter' is used to set model-wide parameters, such as the simulation start time, stop time, and solver settings. This method is beneficial for adjusting settings that impact the entire model. Meanwhile, 'setBlockParameter' is used to set parameters for specific blocks within the model. For example, it allows us to change the gain value of a 'Gain' block or the threshold of an 'If' block. Kindly, refer to the documentation given below for more informations and examples:
3) 'loadVariablesFromMATFile' loads variables into the simulation input object, not directly into the model's workspace. When we run the simulation using 'sim', these variables are used to configure the simulation run. This approach allows us to run multiple simulations with different configurations without changing the model's base workspace.

Categories

Find more on Modeling in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!