MATLAB Answers

How to Programmatically replace blocks in Simulink

조회 수: 16(최근 30일)
Ramae Smith
Ramae Smith 2021년 7월 26일
댓글: Ramae Smith 2021년 8월 2일
I have a model with hundreds of blocks from an old library that I want to replace.
I've made a dummy model. "Version 2" inside the model is the goal. I've done 3 by hand just for clarity.
I want to Replace all Step blocks with Constant blocks. I also want to take the Label of the Step block, and it to the Constant Value of the new Constant block.
There's a couple functions that could work that I've added to the script.
Let me know if you have ideas!

답변(2개)

Shadaab Siddiqie
Shadaab Siddiqie 2021년 7월 29일
Form my understanding you want to replace few blocks in your model with another blocks. You can do this using replace_block like so:
%Load the model sldemo_clutch.
openExample('sldemo_clutch');
%In the 'Unlocked' subsystem, replace blocks whose Gain value is bv with Integrator blocks.
replace_block('sldemo_clutch/Unlocked','Gain','bv','Integrator');
If you want to replace block in model with block from another model you can refer pil_block_replace.
  댓글 수: 1
Ramae Smith
Ramae Smith 2021년 8월 2일
Thanks, this adresses the repalcement aspect.

댓글을 달려면 로그인하십시오.


Ramae Smith
Ramae Smith 2021년 8월 2일
My colleague figured it out. The flaw in this code is that it works one block at a time. Ideally I wanted to replace and relabel all of the step blocks in my model at once.
① Get the name of the Step block
paramValue = get_param ('untitled / Step','Name');
② Convert the Step block to the Constant block
RepNames = replace_block ('untitled','Step','Constant');
(3) Set the value value of the converted Constant block to the name of the Step block.
set_param ('untitled / Step','Value', paramValue);

Community Treasure Hunt

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

Start Hunting!

Translated by