How to sort figure handles in MATLAB?

14 Ansichten (letzte 30 Tage)
MathWorks Support Team
MathWorks Support Team am 16 Feb. 2021
I have an array with multiple figure handles and would like to have them in a sorted order, by ascending figure number.
However when I call the sort function I do not always get the right order, and it changes between MATLAB sessions.
How can I get my figures sorted by their number?
Here is a reproduction example:
>> figs = [figure(1); figure(4); figure(2); figure(3)];
>> s_figs = sort(figs);
The result:
figs = 
  4×1 Figure array:
  Figure    (1)
  Figure    (4)
  Figure    (2)
  Figure    (3)
s_figs = 
  4×1 Figure array:
  Figure    (3)
  Figure    (2)
  Figure    (1)
  Figure    (4)
When I would like:
s_figs = 
  4×1 Figure array:
  Figure    (1)
  Figure    (2)
  Figure    (3)
  Figure    (4)

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 16 Feb. 2021
MATLAB does not sort figure handles based on their numbers but on another unrelated internal property.
One way to sort the figures by their number is to sort the numbers and then index back into your figure array like this:
>> figs = [figure(1); figure(4); figure(2); figure(3)];
>> n_figs = [figs.Number];
>> [~, s_idx] = sort(n_figs);
>> s_figs = figs(s_idx);

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by