yticks function gives error with subplot function
Afficher commentaires plus anciens
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 commentaires
From what you posted we can't deduce what the problem is. The following works, e.g.:
subplot(3,1,1)
x = -3:0.1:3;
f = x;
plot(x,f)
ylim([-3 +3])
yticks([-3 +3])
Try if the code also works on your computer.
Are the arrays "time" and "ttdspfa_3m" properly defined somewhere before ?
Torsten
le 9 Avr 2025
We still don't have "matlab.mat" to load.
"does someone encounter the same error ?"
Yes, very commonly with LENGTH, HEIGHT, etc.
"why does that happen ?"
Because the user creates a variable with the name of a MATLAB function.
Most likely you have a variable named YTICKS. It is very easy to replicate your error using one MAT file:
yticks = pi;
time = 1;
ttdspfa_3m = 2;
save matlab.mat
clearvars
And now your code:
load("matlab.mat");
subplot(3,1,1)
plot(time,ttdspfa_3m,"r","Linewidth",2)
ylim([-3 +3])
yticks([-3 +3])
nado
le 9 Avr 2025
nado
le 9 Avr 2025
nado
le 9 Avr 2025
Réponse acceptée
Plus de réponses (3)
Les Beckham
le 9 Avr 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 commentaires
nado
le 9 Avr 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nado
le 9 Avr 2025
0 votes
Image Analyst
le 9 Avr 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 commentaire
nado
le 9 Avr 2025
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




