reorder on a plot

8 Ansichten (letzte 30 Tage)
MR2022
MR2022 am 12 Jan. 2022
Bearbeitet: MR2022 am 14 Jan. 2022
vComplDire contains randomly on each cell one of these 4 directions:
DownRight; DownLeft; UpRight; UpLeft. (these are simply strings).
vComplDire is loaded by this for loop:
for i = 1:numel(vInterv)
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
boxplot(vInterv, vComplDire);
Boxplotting I get the graphic that I show on the attached pic.
I want to reorder the boxes on the graph like this:
1st DownLeft; 2nd DownRight; 3rd UpLeft; 4th UpRight.
I tried with "reorderlevels", "getlevels", etc... but no success.

Akzeptierte Antwort

Voss
Voss am 12 Jan. 2022
% Generate random data. In order to be able to distinguish the boxes, make
% UpRight have mean 0, DownRight have mean 1, DownLeft have mean 2, and UpLeft have mean 3.
vInterv = randn(100,4)+[0 1 2 3];
vbHori = [true false false true];
vbVert = [true true false false];
vComplDire = cell(1,4);
for i = 1:4
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
vComplDire
vComplDire = 1×4 cell array
{'UpRight'} {'DownRight'} {'DownLeft'} {'UpLeft'}
[~,idx] = ismember({'DownLeft','DownRight','UpLeft','UpRight'},vComplDire)
idx = 1×4
3 2 4 1
boxplot(vInterv(:,idx), vComplDire(idx));
  4 Kommentare
MR2022
MR2022 am 13 Jan. 2022
Bearbeitet: MR2022 am 14 Jan. 2022
As you created it,
having "vInterv = randn(100,4)+[0 1 2 3]; ... etc."
your script works spotless. Fine.
But with my own data it doesn't work.
I probably should have included these details:
vInterv = 1x100 double
vbHori = 1x4 logical
vbVert = 1x4 logical
vComplDire = 1x100 cell (which will include these strings "DownLeft"... etc.)
vInterv has a value on each position, so "for i = 1:4" doesn't work.
I tried your code exactly this way:
for i = 1:numel(vInterv) % this nr. is 100
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
[~,idx] = ismember({'DownLeft','DownRight','UpLeft','UpRight'},vComplDire);
boxplot(vInterv(:,idx), vComplDire(idx));
Voss
Voss am 13 Jan. 2022
When i == 5 you will get an error in the for loop because vbHori only has 4 elements.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by