What does this error mean?
Afficher commentaires plus anciens
I keep getting the error: Subscript indices must either be real positive integers or logicals.
days=10;
dy=-1;
hPlot1 = plot(dy,alt);
while dy<=days
%--calculate & display current time (UTC)
yr=2018;
mnth=1;
dy=dy+1;
hr=0;
min=0;
sc=0;
d=datetime(yr,mnth,dy,hr,min,sc);
%d=datetime('now');
%--covert and display current time in julian days
JD_TT=juliandate(d);
%--convert julian days to moon position
CooType='q2000';
[X,Y,Z]=moonpos(JD_TT,CooType);
%plot3(X,Y,Z);
%--convert to LLA coordinates, in degrees
alt=sqrt(X.^2+Y.^2+Z.^2);
xdata = get(hPlot1,'XData');
ydata = get(hPlot1,'YData');
set(hPlot1,'XData',[xdata dy],'YData',[ydata alt]);
end
altmax=max(ydata);
xmax=find(alt==altmax);
altmin=min(ydata);
xmin=find(alt==altmin);
15 commentaires
Aquatris
le 23 Juil 2018
I am going to guess the error happens inside the "moonpos" function which you did not share. The error means that you are trying to call an index of a vector with a non-positive integer varialbe.
For example if x = [1 2 3 4], and I call x(-1), it will give me the error you are getting.
Walter Roberson
le 23 Juil 2018
Which line is the error occurring on?
Your code does not initialize alt before the initial plot, but does later change alt inside the loop.
Evan Mossel
le 23 Juil 2018
Modifié(e) : Walter Roberson
le 24 Juil 2018
Walter Roberson
le 23 Juil 2018
Examine your overall code more closely. You probably accidentally created a variable named "max".
Evan Mossel
le 24 Juil 2018
Walter Roberson
le 24 Juil 2018
If it is stopping at the line
altmax=max(ydata);
then use
which max
to see whether max is a function or a variable.
Evan Mossel
le 24 Juil 2018
Modifié(e) : Evan Mossel
le 24 Juil 2018
Walter Roberson
le 24 Juil 2018
It would have saved a lot of time if you had posted the complete error message.
Use
which min
to see what it thinks min is.
If you used to have a variable named min but fixed it, then perhaps you have not done
clear min
to get rid of the mistaken version.
Evan Mossel
le 24 Juil 2018
Modifié(e) : Evan Mossel
le 24 Juil 2018
Walter Roberson
le 24 Juil 2018
Your code uses
dy = -1;
so dy is a scalar.
We do not have any information about the initial size of alt: it would be an error if alt was not a scalar.
Evan Mossel
le 24 Juil 2018
Modifié(e) : Evan Mossel
le 24 Juil 2018
Walter Roberson
le 24 Juil 2018
The only place you use alt before recomputing it, is the line
hPlot1 = plot(dy,alt)
which is creating an initial line object that you will use for your plotting. What you could do is
hPlot1 = plot(nan, nan);
Evan Mossel
le 24 Juil 2018
Modifié(e) : Evan Mossel
le 24 Juil 2018
Walter Roberson
le 24 Juil 2018
Your xdata and ydata is going to include that initial nan value.
At the end of your loop, your xdata and ydata are not going to include the last dy and alt, because you change hPolt1's XData and YData after you assign to the temporary variable xdata and ydata.
Evan Mossel
le 26 Juil 2018
Modifié(e) : Evan Mossel
le 26 Juil 2018
Réponses (1)
Krithika A
le 24 Juil 2018
0 votes
You use min in your code. min is already a matlab function
5 commentaires
Evan Mossel
le 24 Juil 2018
Krithika A
le 24 Juil 2018
One of the variables is a non-integer or is negative. What you need to do is go through each line and look at all the variable outputs. If a variable is a non-integer or negative, check the function you are trying to use with that variable. It may be that the function can only use positive integers.
Evan Mossel
le 24 Juil 2018
Krithika A
le 24 Juil 2018
Not specifically for min/max. Min/max can handle non-integers. It would be another function.
Evan Mossel
le 24 Juil 2018
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects 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!