Enabling bus sorting by name in the Simulink Type Editor

How to enable bus sorting by name in the Simulink Type Editor?

4 comentarios

It can be done by
h = Simulink.typeeditor.app.Editor.getInstance;
a = h.getListComp;
a.enableSort;
which doesn't make much sense because it also sorts bus fields.
Is there a way to sort buses, but not fields?
Harimurali
Harimurali el 10 de En. de 2024
Editada: Harimurali el 10 de En. de 2024
Hi Girgorii,
Bus elements are typically displayed in the order in which they are defined or added to the bus object. However, the elements can be manually reordered using a MATLAB script.
Here is an example MATLAB script to sort the bus elements:
%Create bus elements
elems(1) = Simulink.BusElement;
elems(1).Name = 'Sine';
elems(2) = Simulink.BusElement;
elems(2).Name = 'Chirp';
elems(3) = Simulink.BusElement;
elems(3).Name = 'Bees';
%Create the bus object
Sinusoidal = Simulink.Bus;
Sinusoidal.Elements = elems;
% Get the elements of the bus object
elements = Sinusoidal.Elements;
% Sort the elements by name
[~, idx] = sort({elements.Name});
sortedElements = elements(idx);
% Assign the sorted elements back to the bus object
Sinusoidal.Elements = sortedElements;
For nested bus, apply the similar sorting logic recursively to each bus.
Hope this helps.
Grigorii Nefedov
Grigorii Nefedov el 11 de En. de 2024
Editada: Grigorii Nefedov el 11 de En. de 2024
I want to sort buses in Type Editor window, not bus elements.
Mikel
Mikel el 25 de Sept. de 2024
Any updates on this??

Iniciar sesión para comentar.

Respuestas (1)

I understand that you want to enable sorting in “Type Editor” in Simulink. Buses are usually displayed in the order in which they are added in “Type Editor”. One solution is to export the “buses” to a “MAT” file and then sort them programmatically as shown in the below code.
% Load the MAT-file
data = load('data.mat');
varNames = fieldnames(data)
% Sort the variable names alphabetically
[sortedVarNames, sortIdx] = sort(varNames)
sortedData = struct();
for i = 1:length(sortedVarNames)
sortedData.(sortedVarNames{i}) = data.(varNames{sortIdx(i)});
end
% Save the sorted bus objects back to a new MAT-file
save('sorted_data.mat', '-struct', 'sortedData');
disp('Buses sorted and saved to sorted_data.mat');
The next step would be delete all “buses” in the “Type Editor”. Use the “import” option in “Type Editor” to load the data from “sorted_data.mat”. Now the “buses” would be sorted as desired in the “Type Editor” window.
Hope you find this information helpful.

Categorías

Más información sobre Using MATLAB Projects in Simulink en Centro de ayuda y File Exchange.

Productos

Versión

R2023b

Preguntada:

el 9 de En. de 2024

Respondida:

el 30 de Sept. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by