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

2 ビュー (過去 30 日間)
Haroon Zafar
Haroon Zafar 2024 年 1 月 1 日
編集済み: Haroon Zafar 2024 年 1 月 3 日
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 Center および File ExchangeSchedule Model Components についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by