Clone/ Copy a subsystem reference with new set of indexed parameters

조회 수: 2 (최근 30일)
Hi,
I have a large simulink model with multiple instances of a subsytem referenced blocks. Each subsytem referenec has four parameters which are indexed as shown in pictures.
For exampe for block name "E7kW_Long Range27" the parmeters are Chrg_Start_E7L(27), Batt_cap_E7L(27) etc. ie the last two digits of the block name (27) is the index of array defined in worksapce.
Is there any way that I can create a copy/ clone this block with the parameter index automatically updated. ie the copy is created as "E7kW_Long Range28" and parameters as Chrg_Start_E7L(28), Batt_cap_E7L(28) etc. with index updated to 28.
Thanks

채택된 답변

Ayush
Ayush 2024년 1월 2일
I understand that you want to automatically clones a subsystem and updates the parameters based on the block name in Simulink. You can create a MATLAB script to automate this process. The script would copy the subsystem, rename it, and update the parameter settings accordingly. Here is the conceptual code for that:
% Define the base block name and the current index
baseBlockName = 'E7kW_Long Range';
currentIndex = 27;
newIndex = currentIndex + 1;
% Define the subsystem's parent path
parentSystem = 'your_model_name/SubsystemParentPath';
% Define the original and new block paths
originalBlockPath = [parentSystem '/' baseBlockName num2str(currentIndex)];
newBlockPath = [parentSystem '/' baseBlockName num2str(newIndex)];
% Copy the subsystem block to the new location with the new name
add_block(originalBlockPath, newBlockPath);
% Define the parameter names
paramNames = {'Chrg_Start_', 'Batt_cap_', 'ThirdParam_', 'FourthParam_'};
% Update the parameters in the new subsystem
for i = 1:length(paramNames)
% Construct the original and new parameter names
originalParamName = [paramNames{i} 'E7L(' num2str(currentIndex) ')'];
newParamName = [paramNames{i} 'E7L(' num2str(newIndex) ')'];
% Get the original parameter value using get_param
originalValue = get_param(originalBlockPath, 'MaskValues');
% Replace the original parameter name with the new one
newValue = strrep(originalValue, originalParamName, newParamName);
% Set the new parameter value using set_param
set_param(newBlockPath, 'MaskValues', newValue);
end
% Now the new subsystem block has been created and the parameters updated
You may have to update few values in order to suit your requirement.
Thanks,
Ayush
  댓글 수: 1
Haroon Zafar
Haroon Zafar 2024년 1월 3일
편집: Haroon Zafar 2024년 1월 3일
Thanks a lot @Ayush.... It worked perfectly.
Just a little change to make it work as below.
% Define the base block name and the current index
for k=1:10
baseBlockName = 'E7kW_Long Range';
currentIndex = k;
newIndex = currentIndex + 1;
% Define the subsystem's parent path
parentSystem = 'UrbanGenericLV24hrADMD_7kW';
% Define the original and new block paths
originalBlockPath = [parentSystem '/' baseBlockName num2str(currentIndex)];
newBlockPath = [parentSystem '/' baseBlockName num2str(newIndex)];
% Copy the subsystem block to the new location with the new name
add_block(originalBlockPath, newBlockPath);
% Define the parameter names
paramNames = {'Chrg_Start_', 'Eff_','Batt_cap_','SoC_init_'};
% Update the parameters in the new subsystem
for i = 1:length(paramNames)
% Construct the original and new parameter names
originalParamName = [paramNames{i} 'E7L(' num2str(currentIndex) ')'];
newParamName = [paramNames{i} 'E7L(' num2str(newIndex) ')'];
% Get the original parameter value using get_param
originalValue = get_param(originalBlockPath, 'MaskValues');
% Replace the original parameter name with the new one
% newValue = strrep(originalValue, originalParamName, newParamName);
newValue{i} = newParamName
end
% Set the new parameter value using set_param
set_param(newBlockPath, 'MaskValues', newValue);
end
% Now the new subsystem block has been created and the parameters updated

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Schedule Model Components에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by