How to programmatically arrange signals in Bus Creator and Bus Selector blocks alphabetically?
조회 수: 9 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2024년 9월 17일
편집: MathWorks Support Team
2024년 10월 9일
How can I programmatically arrange input signals to the Bus Creator block and output signals from the Bus Selector block alphabetically?
채택된 답변
MathWorks Support Team
2024년 10월 7일
편집: MathWorks Support Team
2024년 10월 9일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Load Signal Data for Simulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!