Too many dimensions error msg in figure

3 views (last 30 days)
I get the message "Error using plot Data may not have more than 2 dimensions" when running the follwoing commands:
Alder = {'<20', '2029','3039','4049','5059','6069','>70', 'all'};
Vars = {AlG, AlR, AlI, AlDI, AlBV};
Vars2 = {'AlG', 'AlR', 'AlI', 'AlDI', 'AlBV'};
Aar = {'2011', '2012', '2013_1', '2014_1', '2014_s2', '2014_s5'};
n = length(Vars);
nAar = length(Aar);
nAl = length(Alder);
numberOfColors = nAl;
myColorMap = lines(numberOfColors);
for iVars = 1:n;
aVars = Vars{iVars};
figure,title(Vars2{iVars});
hold on
for iAl = 1:nAl
plot(aVars(11,iAl,:), 'color', myColorMap((iAl), :));
set(gca, 'XTickLabel',Aar)
legend((Alder),'location','NE','FontSize',10);
end
hold off
end
plot(aVars(11,1,:), 'color', myColorMap((iAl), :));
All matrices inn "Vars" is 11x8x6 matrices. I don't understand why the above commands fails, since almost the same commands work in this case below. The only difference is that in the above script I have "Alder" on the x-axis and in the below script I have "Aar" on the x-axis.
Alder = {'<20', '2029','3039','4049','5059','6069','>70', 'all'};
Vars = {gjG, gjR, gjI, gjDI, gjBV, GtoDI, GtoBV, RtoDIplusR, ItoG, RtoG, GtoI};
Vars2 = {'gjG', 'gjR', 'gjI', 'gjDI', 'gjBV', 'GtoDI', 'GtoBV', 'RtoDIplusR', 'ItoG', 'RtoG', 'GtoI'};
Aar = {'2011', '2012', '2013_1', '2014_1', '2014_s2', '2014_s5'};
n = length(Vars);
nAar = length(Aar);
numberOfColors = 6;
myColorMap = lines(numberOfColors);
for iVars = 1:n;
aVars = Vars{iVars};
figure,title(Vars2{iVars});
hold on
for iAar = 1:nAar
plot(aVars(11,:,iAar), 'color', myColorMap((iAar), :));
set(gca, 'XTickLabel',Alder)
legend((Aar),'location','NE','FontSize',10);
end
hold off
end

Accepted Answer

Walter Roberson
Walter Roberson on 27 May 2013
plot( squeeze(aVars(11,iAl,:)), 'color', myColorMap((iAl), :));
In the first one you had a 3D matrix accessed at (specific value, specific value, :) . In MATLAB the result of that would be 1 x 1 x N, a 3D result. MATLAB cannot plot 3D vectors.
In the second one you had a 3D matrix accessed at (specific value, :, specific value). In MATLAB the result of that would be 1 x N x 1 and then the trailing 1's get dropped, leaving 1 x N which is a 2D result; MATLAB is able to plot with 2D vectors.
The squeeze() I added above transforms the 3D vector into a 2D vector.

More Answers (0)

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!