Why do I get error for xlabel all of a sudden?
Show older comments
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 Comments
The legend is just a matter of using the right array type:
legend([t3, t5], {t3_name, t5_name});
The issue with xlabel() is probably because something is shadowing it. Possibly a variable or another file called "xlabel". You can find what MATLAB thinks xlabel() is:
which xlabel -all
If the results of running which aren't pointing to that file, then you've found the problem. For instance:
xlabel = 'this is what i call my x-axis';
which xlabel -all
Nobutaka Kim
on 7 Feb 2022
Edited: Nobutaka Kim
on 7 Feb 2022
If your output for which() is the same as the first example:
/MATLAB/toolbox/matlab/graph2d/xlabel.m
and nothing else, then there shouldn't be a problem.
If you get something like the second example, then the extraneous entry is the problem itself.
To demonstrate:
xlabel = 'this is what i call my x-axis'; % xlabel isn't a function anymore
xlabel('x') % this isn't a function call; it's indexing into a vector
This is happening because 'xlabel' is now a variable. When you try to call the function by the same name, you're actually indexing into the variable called xlabel. To be specific, it converts the char vector 'x' into its ASCII numeric value (120) and tries to retrieve the 120th element of the vector.
The solution is to find the variable called 'xlabel' and name it something else.
DGM
on 7 Feb 2022
That's a good point. It's always a good idea to keep the workspace clear of old variables from prior or unrelated code.
Accepted Answer
More Answers (1)
Nobutaka Kim
on 8 Feb 2022
0 votes
Categories
Find more on Title 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!