How to programmatically arrange signals in Bus Creator and Bus Selector blocks alphabetically?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 17 Sep 2024
Edited: MathWorks Support Team
on 9 Oct 2024
How can I programmatically arrange input signals to the Bus Creator block and output signals from the Bus Selector block alphabetically?
Accepted Answer
MathWorks Support Team
on 7 Oct 2024
Edited: MathWorks Support Team
on 9 Oct 2024
There is no exact API to do such rearrangement, however, you can refer to the below example code to create your own script to achieve such rearrangement:
% Load the Simulink model
model = 'test';
load_system(model);
% Specify the Bus Creator block path
busCreatorBlock = strcat(model, '/Bus Creator');
% Get the InputsString property of the Bus Creator block
inputsString = get_param(busCreatorBlock, 'InputsString');
% Split the InputsString into individual signal names
signalNames = strsplit(inputsString, ',');
% Trim any whitespace from signal names
signalNames = strtrim(signalNames);
% Sort the signal names alphabetically
sortedSignalNames = sort(signalNames);
% Join the sorted signal names back into a comma-separated string
sortedInputsString = strjoin(sortedSignalNames, ',');
% Set the InputsString property with the sorted signal names
set_param(busCreatorBlock, 'InputSignals', sortedInputsString);
% Auto arrange the system
Simulink.BlockDiagram.arrangeSystem(model);
% Save and close the model
save_system(model);
% Sort Bus Selector OutputSignals.
% Assume Bus selector block is selected in canvas,
% or pass the proper handle in place of 'gcbh'
% Get the output signals as a string
sig = get_param(gcbh, 'OutputSignals');
% Split the string into individual signal names
signalList = split(sig, ',');
% Trim any whitespace from signal names
signalList = strtrim(signalList);
% Sort the signal names alphabetically
sortedSignalList = sort(signalList);
% Concatenate the sorted signal names back into a single string
sortedSig = strjoin(sortedSignalList, ',');
% Display the sorted output signals
disp(sortedSig);
% set sortedSig to block handle
set_param(gcbh, 'OutputSignals', sortedSig);
Arranging the input signals in a Bus Creator block would result in the misalignment of signals and associated blocks in the Simulink model. In such cases, you use Simulink.BlockDiagram.arrangeSystem to improve the layout of the specified block diagram by realigning, resizing, and moving blocks and straightening signal lines.
Please refer to the below link to know more details about the same:
Improve layout of block diagram:
0 Comments
More Answers (0)
See Also
Categories
Find more on Load Signal Data for Simulation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!