yticks function gives error with subplot function

4 views (last 30 days)
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
nado
nado on 9 Apr 2025
It works for me now !
i dont know what was happening.
Sorry for this inconvenience and for wasting your time.
Thank you for taking the time to try my code and searching for the source of error.

Sign in to comment.

Accepted Answer

Star Strider
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
/MATLAB/toolbox/matlab/graphics/graphics/graph3d/yticks.m
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
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
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.

Sign in to comment.

More Answers (3)

Les Beckham
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
nado
nado on 9 Apr 2025
  • Thank you for yor quick response.
  • I have tried in the command window:
which -all yticks
  • It returns
C:\Program Files\MATLAB\R2021a\toolbox\matlab\graph3d\yticks.m
  • Here is a screenshot
  • Best Regards
Les Beckham
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Sign in to comment.


nado
nado on 9 Apr 2025
It works for me now !
i dont know what was happening.
Sorry for this inconvenience and for wasting your time.
Thank you for taking the time to try my code and searching for the source of error.

Image Analyst
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.
  1 Comment
nado
nado on 9 Apr 2025
Thank you for your reply.
Sorry for this inconvenience and for wasting your time.
Honestly,i dont know what was happening. Anyway, it works now.
Thank you for sharing the methodology to find the error. I have not used the breakpoint feature before but i will in the future.
Thank you also for taking the time to try my code and searching for the source of error and suggesting the use of a breakpoint.
Best Regards,

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!