Programmatically comment blocks residing in a subsystem

1 view (last 30 days)
Hi,
I am trying to programmatically comment some subsystem blocks in my model. Here I have shown a simplified version of in the attached model file. ( actual number of subsystem block is in 100s)
Subsytem1 has: E7kW_Medium Range1, E7kW_Long Range1
Subsytem2 has: E7kW_Medium Range2 , E7kW_Long Range2
clc
clear all
open_system('Commenting_Programmatic')
Num_7L=2
Num_7M= 2
Status_7L= 'on'; % Comment status
Status_7M= 'on';
for kk= 1: Num_7L
set_param([gcb,'/E7kW_Long Range',num2str(kk),''],'Commented',Status_7L)
end
for ll= 1: Num_7M
set_param([gcb,'/E7kW_Medium Range',num2str(ll),''],'Commented',Status_7M)
end
E7kW_Medium Range1 gets commented out, but E7kW_ Medium Range2 ( residing in Subsystem2) gives error , as the gcb still reads the previous location w.rt Subsystem1.
If I put the EV subsystem in model root, then commenting works fine. But how can I get address of the block when it is residing in separate subsystems?
Thanks,
Haroon

Accepted Answer

Haroon Zafar
Haroon Zafar on 24 Nov 2023
Corrected code with asociated simulink file is here:
clc
clear all
load_system('Commenting_Programmatic')
Num_7L=2
Num_7M= 2
Status_7L= 'on'; % Comment status
Status_7M= 'off';
for kk= 1: Num_7L
EV_7L_names= strcat('E7kW_Long Range',num2str(kk));
fullnames= getfullname(Simulink.findBlocks('Commenting_Programmatic','Name',EV_7L_names));
path= string(fullnames);
set_param(path,'Commented',Status_7L)
end
for ll= 1: Num_7M
EV_7M_names= strcat('E7kW_Medium Range',num2str(ll));
fullnames= getfullname(Simulink.findBlocks('Commenting_Programmatic','Name',EV_7M_names));
path= string(fullnames);
set_param(path,'Commented',Status_7M)
end

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 20 Nov 2023
If doing it programmingly, never use "gcb" or "gcs". Instead, use the full block path like "RootModelName/SubSystemName/BlockName". If the "SubSystemName" is varying, then use find_system() to find the SubSystem block and get its name.
  1 Comment
Haroon Zafar
Haroon Zafar on 24 Nov 2023
Thanks for the comment. But I was not able to find the specific varying subsystem name using find_system.
Though Simulink.findBlocks did the job.
Corrected code with asociated simulink file is here:
clc
clear all
load_system('Commenting_Programmatic')
Num_7L=2
Num_7M= 2
Status_7L= 'on'; % Comment status
Status_7M= 'off';
for kk= 1: Num_7L
EV_7L_names= strcat('E7kW_Long Range',num2str(kk));
fullnames= getfullname(Simulink.findBlocks('Commenting_Programmatic','Name',EV_7L_names));
path= string(fullnames);
set_param(path,'Commented',Status_7L)
end
for ll= 1: Num_7M
EV_7M_names= strcat('E7kW_Medium Range',num2str(ll));
fullnames= getfullname(Simulink.findBlocks('Commenting_Programmatic','Name',EV_7M_names));
path= string(fullnames);
set_param(path,'Commented',Status_7M)
end

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!