How to set Simulink block parameters using an absolute model path

조회 수: 11 (최근 30일)
Daniel
Daniel 2025년 1월 30일
댓글: Daniel 2025년 1월 31일
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에서 Programmatic Model Editing에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by