How to set Simulink block parameters using an absolute model path
8 views (last 30 days)
Show older comments
I am calling a simulink model out of a MATLAB script, but this model is located in a different directory (not in the working directory).
This model can be executed by using the "sim" command in combination with the absolute path, e.g:
sim("C:\Users\MATLAB\myModel.slx")
Now I want to change the parameter of a "Contant" block when calling the simulation.
The functions "set_param" seems only to work with a model handle which was created by a model being in the current working path.
% name of model
mdl = "myMdl";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
When performing the same action, but with an absolute path, this is not working anymore.
% name of model
mdl = "C:\Users\MATLAB\myModel.slx";
% name of the block
blk = "myConstBlk";
% Serch path
path = strcat(mdl, "/", blk);
% set parameter of Const
set_param(path, "Value", "5");
Invalid Simulink object name: 'C:Users\MATLAB\myMdl.slx/myConstBlk'.
Even if the model is loaded by:
h = load_system(mdl); %mdl is the absolute path
the handel "h" could not be used in the set_param function.
So therefore the qustion:
How to perform all the task provided by "set_param" and "get_param" using a model which shall be referenced by an absolute path?
2 Comments
Oliver Jaehrig
on 30 Jan 2025
Edited: Oliver Jaehrig
on 30 Jan 2025
Why are you trying to do this at all (I mean using the absolute path)?
The model in memory which can be referenced by using the name of the file (in your case myMdl) can be used with set_param and get_param as you already mentioned, so it should not be needed to do this.
Also:
"the handel "h" could not be used in the set_param function."
the handle should work. What error message do you get? Could it be that the issue is, that you are trying to concatenate the handle with a block path?
Accepted Answer
Fangjun Jiang
on 30 Jan 2025
Edited: Fangjun Jiang
on 30 Jan 2025
The "absolute model path" of a block starts with the name of the model, not including the file folder where the model file is stored.
so typically,
open_system("C:\Users\MATLAB\myModel.slx")
get_param("myModel\myConstBlk",'Value')
More Answers (0)
See Also
Categories
Find more on Programmatic Model Editing 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!