How to add axis labels while using for loops? Keep getting error: Index exceeds the number of array elements (4).

조회 수: 6 (최근 30일)
I am getting an error trying to add axis titles to my plot. I am using a for loop, but it is only letting me add a legend and a title.
this is the error message:
Index exceeds the number of array elements (4). Error in Homework_1 (line 12) xlabel('real')
z = [2 + 5i; 1.41+1.41i; 5.87+9.15i; -3.07+6.72i; 0.997+0.071i; 3*exp(1.23i); 2*exp((pi/2)*1i); 13*exp(1i); 3; 2+11i; 4+2i; (1/5)+(4/5)*1i; ] ;
s = ['o'; '*'; 's'; 'p'; '+'; '>'; '<'; 'x'; 'd'; '.'; 'h'; '^';];
for index = 1:12
plot (z(index), s(index), 'MarkerSize', 10 );
hold on;
end
hold off;
title('complex numbers');
legend('2 + 5i', '1.41+1.41i', '5.87+9.15i', '-3.07+6.72i', '0.997+0.071i', '3*exp(1.23i)', '2*exp((pi/2)*1i)', '13*exp(1i)', '3', '2+11i', '4+2i', '(1/5)+(4/5)*1i');
xlabel('real');
ylabel('imaginary');
  댓글 수: 2
woahs
woahs 2020년 1월 19일
Not sure what the issue is here. The provided code executes without error. Your axis properties are set outside the loop so the fact that you used a loop to create them shouldn't matter.
Kendall Noto
Kendall Noto 2020년 1월 19일
when i run this script is does not add the labels to my resulting plot
Annotation 2020-01-19 142043.png
while giving me this error message:
Annotation 2020-01-19 142132.png

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
Updated answer
It appears that you may have a variable named xlabel that is interfering with the xlabel() function. To confirm this, run the line below.
which xlabel
If my hunch is correct, it should output: xlabel is a variable.
-----------------------------------------------------------------------
Old answer (still relevant)
As Kevin Chen mentioned, the code you shared does not result in any errors.
Here are 3 improvements to make.
  1. Define number of iterations based on number of values available
  2. Use DisplayName to label the legend
z = [2 + 5i; 1.41+1.41i; 5.87+9.15i; -3.07+6.72i; 0.997+0.071i; 3*exp(1.23i); 2*exp((pi/2)*1i); 13*exp(1i); 3; 2+11i; 4+2i; (1/5)+(4/5)*1i; ] ;
s = ['o'; '*'; 's'; 'p'; '+'; '>'; '<'; 'x'; 'd'; '.'; 'h'; '^';];
% Define legend strings
legStr = {'2 + 5i', '1.41+1.41i', '5.87+9.15i', '-3.07+6.72i', '0.997+0.071i', '3*exp(1.23i)', '2*exp((pi/2)*1i)', '13*exp(1i)', '3', '2+11i', '4+2i', '(1/5)+(4/5)*1i'};
for index = 1:numel(z) % use z or s
plot (z(index), s(index), 'MarkerSize', 10, 'DisplayName',legStr{index}); % use DisplayName
hold on;
end
hold off;
title('complex numbers');
legend(); % just call legend()
xlabel('real');
ylabel('imaginary');
You could also move "hold on" outside of the loop.
  댓글 수: 8
Kendall Noto
Kendall Noto 2020년 1월 19일
Ohhh, i do remember now that i entered the xlabel wrong when i tried to do it at the very beginning. I understand now. Thank you so much for your help!!!
Adam Danz
Adam Danz 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
One way to avoid these types of errors is to use functions rather than scripts. Functions have their own workspaces so if you accidentally created a variable named xlabel from the command window, it wouldn't exist within the function workspace the next time the function was run.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by