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

11 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 21 Nov 2017
Modificato: Fangjun Jiang il 21 Nov 2017
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 Commenti
Abhijit Das
Abhijit Das il 24 Mar 2020
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 il 24 Mar 2020
Modificato: Fangjun Jiang il 24 Mar 2020
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.

Accedi per commentare.

Più risposte (1)

KL
KL il 21 Nov 2017
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');

Categorie

Scopri di più su Interactive Model Editing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by