Programmatically change user defined Library objects in Simulink model

Dear Sirs,
I have made a plain Simulink model consisting of Current Sensor/Scope/Switch etc. There are also two subsystems (Load and Battery) that are user defined Library objects. Links looks fine. The model works and simulation results are correct.
Now I want to programmatically (from Matlab command window) change the Library objects. Say from Load_RLC to Load_R. Both exists as Library objects. I have tried commandos like:
open_system('LowVoltageSystem')
add_block('Load_R','LowVoltageSystem')
pos=get_param('Load_RLC/LowVoltageSystem,'position')
delete_block('Load_RLC/LowVoltageSystem’)
set_param('Load_R','position',pos)
But the ‘Load_R’ can not be found (There is no block named 'Load_R')
I have also tried things like:
replace_block('LowVoltageSystem,'Load_RLC','Load_R')
Here the ‘Load_R’ is invalid block type.(Invalid NewBlock (<Load_R>) passed to REPLACE_BLOCK. Try built-in/BLOCKTYPE or a valid block name.)
The replace_block command, however, works with the built-in library blocks.
If I change the library components ‘manually’ everythings works, so there is nothing wrong with the components themselves.
What am I doing wrong here?
Best//Gert

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2021년 5월 27일
편집: Fangjun Jiang 2021년 5월 27일

1 개 추천

In the first part of your code, add_block() is not used properly. You need to specify the full path of both the source and destination blocks. Look at examples in "doc add_block".
replace_block() is the ideal function to use for your task. Again, you need to specify the full path of the source and desination blocks, e.g. 'CustomLibraryFile/LibraryBlock', 'ModelFile/ModelSystem/BlockName'. For custom library, you also need to open the library first using open_system() or load_system()

댓글 수: 4

Maybe there is something that I do not understand when it comes to description of path?
Block name still seems not valid?
The full path of a block is 'ModelFileName\SystemBlockName\BlockName'.
However, replace_block() requires this info in a slightly different way. Look at the examples in "doc replace_block"
Most likely:
replace_block('LowVoltageSystem','Name','Load_RLC', 'Load_R/Load_R')
You are perfectly right!
I will make the model more clear by renaming the load in 'LowVoltagesystem' to 'Load'. I will also put the load in a library 'Load_Lib'. The command must then become:
replace_block('LowVoltageSystem','Name','Load', 'Load_Lib/Load_R')
Following the LibraryLink of the Load in the Simulink model 'LowVoltagesystem' then shows what Load is active.
If I remember right there was also an option for closing the "Replace Dialog" that appears. Nice things todo tomorrow :-)
Thanks a lot for your professional help!
To suppress the dialog box from being displayed, add the 'noprompt' argument as the last argument in the command.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

질문:

2021년 5월 27일

댓글:

2021년 5월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by