yticks function gives error with subplot function
4 views (last 30 days)
Show older comments
Hello everyone,
I am trying to use the function subplot in matlab.
however, when i specify the axes property "yticks", it gives me an error.
does someone encounter the same error ? why does that happen ?
Here is my code (I was obliged to comment the code since it is automatically executed)
%%%%%%%%%%%%%%%%%%%%%% START of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load("matlab.mat");
% subplot(3,1,1)
% plot(time,ttdspfa_3m,"r","Linewidth",2)
% ylim([-3 +3])
% yticks([-3 +3])
% subplot(3,1,2)
% plot(time,ttdspfa_0m,"r","Linewidth",2)
% ylabel("Tower top fore-aft displacement (m)")
% subplot(3,1,3)
% plot(time,delta_ttdspfa,"r","Linewidth",2)
% xlabel("Time (s)")
%%%%%%%%%%%%%%%%%%% END of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Here is the error i obtain
%%%%%%%%%%%%%%%%%%%%%% START of the error %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Array indices must be positive integers or logical values.
Error in Verif_1 (line 5)
yticks([-3 +3])
%%%%%%%%%%%%%%%%%%% END of the error %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thank you in advance.
Best Regards
6 Comments
Accepted Answer
Star Strider
on 9 Apr 2025
Edited: Star Strider
on 9 Apr 2025
You probably have a variable named ‘yticks’.
Run this in a script or your command window —
which yticks
If you get anything other than that, specifically something like:
yticks is a viariable.
that will point to the problem.
The solution is to re-name the variable to something that fits the context of your code and does not overshadow a MATLAB function.
.
6 Comments
Star Strider
on 9 Apr 2025
Also, you definittely did not waste my time!
I always learn something from every question I answer.
Image Analyst
on 9 Apr 2025
Sorry you never discovered the root cause of the error.
Like I suggested in my answer, my best guess is that, somewhere in the code you did not share with us, you created a variable by doing this
yticks = [-3 +3];
thinking that would call the yticks function, or basically have the same effect as calling the function. Then, later, when you really tried to call the yticks function (using parentheses) it didn't work because it was referencing the variable you created instead of calling the function.
More Answers (3)
Les Beckham
on 9 Apr 2025
It sounds like you have defined a variable named yticks at some point in the past and Matlab is trying to index into it with the vector [-3 +3], thus the error because -3 is not a valid index.
You can confirm this by entering this in the command window:
which -all yticks
If you see
yticks is a variable.
C:\Program Files\MATLAB\R2025a\toolbox\matlab\graphics\graphics\graph3d\yticks.m % Shadowed
Instead of just this
C:\Program Files\MATLAB\R2025a\toolbox\matlab\graphics\graphics\graph3d\yticks.m
then clear the variable as follows and try running your code again
clear yticks
2 Comments
Les Beckham
on 9 Apr 2025
Seems to work here. Is it still not working for you?
%%%%%%%%%%%%%%%%%%%%%% START of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load("matlab.mat");
subplot(3,1,1)
plot(time,ttdspfa_3m,"r","Linewidth",2)
ylim([-3 +3])
yticks([-3 +3])
subplot(3,1,2)
plot(time,ttdspfa_0m,"r","Linewidth",2)
ylabel("Tower top fore-aft displacement (m)")
subplot(3,1,3)
plot(time,delta_ttdspfa,"r","Linewidth",2)
xlabel("Time (s)")
%%%%%%%%%%%%%%%%%%% END of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Image Analyst
on 9 Apr 2025
Like I and everyone else says, your code runs for us and the error message indicates you have a variable called yticks. Set a breakpoint on that line and run it and when it stops there, check the workspace panel and see if you see yticks in there. If it is, and it will be, then you are just supplying us with a snippet of code, not your whole code. For example, maybe you set yticks somewhere else using
yticks = [-3, 3]; % Creates a variable.
instead of calling the function
yticks([-3, 3]);
or maybe you set a global variable somewhere and you're not showing that code to us.
Regardless, since you don't believe you have a yticks variable then you must not need it so clear it. Even if it doesn't exist, you can clear it. So I'm pretty sure this code will work:
%%%%%%%%%%%%%%%%%%%%%% START of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load("matlab.mat")
subplot(3,1,1)
plot(time,ttdspfa_3m,"r","Linewidth",2)
ylim([-3 +3])
% ----------------- New code:
yticks % See what values it has
whos yticks % See what it is
clear yticks % Get rid of existing variable.
% clear global yticks; % If it's a global variable.
%----------------------------------------
yticks([-3 +3])
subplot(3,1,2)
plot(time,ttdspfa_0m,"r","Linewidth",2)
ylabel("Tower top fore-aft displacement (m)")
subplot(3,1,3)
plot(time,delta_ttdspfa,"r","Linewidth",2)
xlabel("Time (s)")
%%%%%%%%%%%%%%%%%%% END of the code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Let me know if it doesn't. Your yticks will put up only 2 tick marks. Why don't you just try getting rid of the yticks() line altogether? Let it set them automatically -- you don't need to set them manually.
See Also
Categories
Find more on Axis Labels 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!