How to set Simulink block parameters using an absolute model path

8 visualizaciones (últimos 30 días)
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 comentarios
Oliver Jaehrig
Oliver Jaehrig el 30 de En. de 2025
Editada: Oliver Jaehrig el 30 de En. de 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?
Daniel
Daniel el 31 de En. de 2025
"Why are you trying to do this at all (I mean using the absolute path)? "
The model is just stored in a different path then the current working directory because it was originally created for an older project. Now I am working on a new project (which is located in a different folder) but the "old" model should be executed in the current project in order to compare some data of this "old project" to the "new project".
The solution provided below by "Fangjun-Jiang" works fine, so thanks anyway!

Iniciar sesión para comentar.

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 30 de En. de 2025
Editada: Fangjun Jiang el 30 de En. de 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')
  1 comentario
Daniel
Daniel el 31 de En. de 2025
Perfect, thanks for the simple solution. So first calling "open_system"and then just working with the model name like being a local model in the working directory.
Works fine!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by