How can I change the color of all similar blocks in a complex simulink system programmatically?

27 次查看(过去 30 天)
I have multi level simulink system with masked subsystems and libraries. The previous owners have the coloring jumbled and I want to write a script to find all blocks of the same type and change the color of those blocks.
Think of this as enforcing styling guidelines.

采纳的回答

Fangjun Jiang
Fangjun Jiang 2017-11-21
编辑:Fangjun Jiang 2017-11-21
Something like this but modifying blocks inside library links is tricky. I would suggest you modify the library directly. Open the library model, unlock it and run the script against the library model file. Then all the library links in your model should be updated automatically.
%%load the example f14 model
f14;
Model='f14';
GainBlks=find_system(Model,'FindAll','On','LookUnderMasks', 'All','BlockType','Gain');
set(GainBlks,'BackgroundColor','red');
  2 个评论
Abhijit Das
Abhijit Das 2020-3-24
Thanks. Just wanted to make sure if I understand the concept right. So, if I change the color of a custom library block, it should affect the same block color in the model, right? If that is the case, then I am not seeing the same with my model and library model. When I changed the background color of my library blocks, it did not change the color in the model. I checked the links and everything looks normal. Let me know if that is expected. Thank you!
Fangjun Jiang
Fangjun Jiang 2020-3-24
编辑:Fangjun Jiang 2020-3-24
You have a block in your library. If you change the color of the block itself and drag an instance to your model, the instance will have the new color. However, the previously existing instances in your model won't change to the new color. I assume that is because the color (probably some other properties such as font and position) is not a property important to the library element. In fact, you can change the color of the instances in your model, it is allowed and it won't require to update to the library. Same thing is true for size. We change the size of the block in the model all the time.
If the block in the library is a subsystem and you changed the color of one of the blocks inside the subsystem, all the instances (old or new) in your model will have the new color for that internal block.

请先登录,再进行评论。

更多回答(1 个)

KL
KL 2017-11-21
use findblocks to get all blocks of your model,
and then apply if else or your logic to separate your blocks and use setparam and BackgroundColor property like,
set_param(gcb,'BackgroundColor','red');

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by