Clone/ Copy a subsystem reference with new set of indexed parameters
6 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Ayush
on 2 Jan 2024
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
More Answers (0)
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!