xlabel and ylabel errors

조회 수: 2 (최근 30일)
winfrida mwigilwa
winfrida mwigilwa 2025년 2월 17일
댓글: Walter Roberson 2025년 2월 17일
I have the following code:
x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
xlabel('-2\pi < x < 2\pi')
ylabel('Sine and Cosine Values')
when I run it brings the following error
Index exceeds the number of array elements. Index must not exceed 3.
Error in mfano (line 8)
ylabel('Sine and Cosine Values')
  댓글 수: 1
Paul Bower
Paul Bower 2025년 2월 17일
Hi Winfrida, Just FYI ... I ran your code on version R2024b and it works fine.

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

채택된 답변

Star Strider
Star Strider 2025년 2월 17일
This is instructive:
when I run it brings the following error
Index exceeds the number of array elements. Index must not exceed 3.
Error in mfano (line 8)
ylabel('Sine and Cosine Values')
Check to see if you have a variable named ‘ylabel’.
To do that, run this from a script or your Command Window —
which ylabel -all
/MATLAB/toolbox/matlab/graphics/graphics/axis/ylabel.m /MATLAB/toolbox/matlab/graphics/graphics/specgraph/+matlab/+graphics/+chart/Chart.p % matlab.graphics.chart.Chart method /MATLAB/toolbox/shared/controllib/graphics/+controllib/+chart/BodePlot.m % controllib.chart.BodePlot method /MATLAB/toolbox/shared/controllib/graphics/+controllib/+chart/+internal/+foundation/AbstractPlot.m % controllib.chart.internal.foundation.InputOutputPlot method
You should only get results similar to these. If the first entry is:
ylabel is a variable.
you have identified the problem. The solution is to re-name the variable.
.
  댓글 수: 3
Star Strider
Star Strider 2025년 2월 17일
As always, my pleasure!
Walter Roberson
Walter Roberson 2025년 2월 17일
It is somewhat common to code, for example
ylabel = ('Sine and Cosine Values')
which creates a variable named ylabel instead of calling the function named ylabel

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by