How to set Simulink block parameters using an absolute model path

2 次查看(过去 30 天)
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 个评论
Oliver Jaehrig
Oliver Jaehrig 2025-1-30
编辑:Oliver Jaehrig 2025-1-30
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 2025-1-31
"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!

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2025-1-30
编辑:Fangjun Jiang 2025-1-30
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 个评论
Daniel
Daniel 2025-1-31
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!!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Subsystems 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by