How do i fix line length errors in matlab
Ältere Kommentare anzeigen
I amtrying to create a figure in which I am to insert lengths (1 : length) but i keep getting errors:
Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side
is 1-by-3.
clear YTick1 YLabel
YTick1 = [ -20:20:140 ]
for i=1:length(YTick1)
if YTick1(i) == -20
YLabel1(i:i,1:4) = [ num2str(YTick1(i), '%4.0f') ];
elseif YTick1(i) > -20 & YTick1(i) < 0
YLabel1(i:i,1:3) = [ num2str(YTick1(i), '%3.0f') ];
elseif YTick1(i) == 0
YLabel1(i:i,1:1) = [ num2str(YTick1(i), '%1.0f') ];
elseif YTick1(i) > 0
YLabel1(i:i,1:2) = [ num2str(YTick1(i), '%2.0f ') ];
end
end
clear YTick2 YLabel2
YTick2 = [ -20:20:140 ]
for i=1:length(YTick2)
if YTick2(i) < -20 & YTick2(i) < 0
YLabel2(i:i,1:4) = [ num2str(YTick2(i), '%4.0f') ];
elseif YTick2(i) > -20 & YTick2(i) < 0
YLabel2(i:i,1:3) = [ num2str(YTick2(i), '%3.0f') ];
elseif YTick2(i) == 0
YLabel2(i:i,1:1) = [ num2str(YTick2(i), '%1.0f') ];
elseif YTick2(i) > 0 & YTick2(i) < 140
YLabel2(i:i,1:2) = [ num2str(YTick2(i), '%2.0f') ];
end
end
Antworten (1)
If I understand correctly what you are trying to do, it is to fill in a 2D character array YLabel1, each row of which corresponds to a label of one YTick, which are known.
Instead of specifying the number of characters each label should have, based on the tick value, you can use a more general format in num2str, then see how long the resulting label is, and put it in place after its length is known:
YTick1 = [ -20:20:140 ]
for i=1:length(YTick1)
str = num2str(YTick1(i), '%.0f');
YLabel1(i,1:numel(str)) = str;
end
YLabel1
By the way, it may be more convenient to use a cell array YLabel1 rather than a 2D character array:
YLabel1 = sprintfc('%.0f',YTick1)
Kategorien
Mehr zu Axis Labels finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!