Why do I get error for xlabel all of a sudden?

123 ビュー (過去 30 日間)
Nobutaka Kim
Nobutaka Kim 2022 年 2 月 7 日
回答済み: Nobutaka Kim 2022 年 2 月 8 日
Index exceeds the number of array elements. Index must not
exceed 10.
Error in readData (line 57)
xlabel('x');
I have a data.m with 8 rows and 3000 columns.
My plots were "working" (slight problem with the legend not showing for second line but good enough) but now I am getting this on Line 57.
I attached my script but it looks like this around line 57
%plot xy for task3 and 5
figure; hold on
%plot(task3_x, task3_y, task5_x, task5_y);
t3 = plot(task3_x, task3_y);
t3_name = 'task 3';
t5 = plot(task5_x, task5_y);
t5_name = 'task 5';
title('task 3 and 5');
xlabel('x'); % line 57
ylabel('y');
legend([t3, t5], [t3_name, t5_name]);
  5 件のコメント
Voss
Voss 2022 年 2 月 7 日
編集済み: Voss 2022 年 2 月 7 日
"The solution is to find the variable called 'xlabel' and name it something else."
Yes, or put a clear (or clear xlabel) at the top of your script readData.m.
DGM
DGM 2022 年 2 月 7 日
That's a good point. It's always a good idea to keep the workspace clear of old variables from prior or unrelated code.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2022 年 2 月 8 日
@Nobutaka Kim, The code does not throw that error for me. So like DGM says, it's likely you ran a different script that placed a variable called xlabel into the base workspace so when this script runs, it throws an error because it thinks xlabel is an array instead of the function to label an axis. To debug, put this line in there. Either early in your script, or just type it into the command window:
which -all xlabel
Tell me what you see in the command window.

その他の回答 (1 件)

Nobutaka Kim
Nobutaka Kim 2022 年 2 月 8 日
Thanks all for the kind advice.
I restarted Matlab and the error went away so it must have been a variable in the workspace as mentioned.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by