What does this error mean?

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
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
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
Evan Mossel le 23 Juil 2018
Modifié(e) : Walter Roberson le 24 Juil 2018
Do you think I need to initialize it? It worked fine before the last 4 lines were added. No other changes were made. The last 4 lines caused this error.
Walter Roberson
Walter Roberson le 23 Juil 2018
Examine your overall code more closely. You probably accidentally created a variable named "max".
I could not find it. But while this thread is active, I am getting another error that states:
Error in MoonPosFK5 (line 37)
altmin=min(alt);
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
Evan Mossel le 24 Juil 2018
Modifié(e) : Evan Mossel le 24 Juil 2018
It returned this, which I assume means it is a function. And I believe it is stopping on the 'altmin' line.
built-in (C:\Program Files\MATLAB\R2015b\toolbox\matlab\datafun\@logical\max) % logical method
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
Evan Mossel le 24 Juil 2018
Modifié(e) : Evan Mossel le 24 Juil 2018
I apologize that I did not post the complete error message. I did do clear min and all went bad. I am getting this error after doing 'clear min':
Error using plot
Vectors must be the same length.
Error in MoonPosFK5 (line 5)
hPlot1 = plot(dy,alt);
Update: it worked I made a typo upon re-coding an in correct line. Thank you! Unfortunately this wasn't an answer so I can't accept it.
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
Evan Mossel le 24 Juil 2018
Modifié(e) : Evan Mossel le 24 Juil 2018
Could I initialize alt as a scalar without assigning it an initial value? If I assign alt a starting value, it will not be an accurate first point. But if I set it to 0, then 0 will always be the minimum, even though this is not correct either.
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
Evan Mossel le 24 Juil 2018
Modifié(e) : Evan Mossel le 24 Juil 2018
Fantastic! Thank you so much. Now to figure out why my altmax and altmin only display the final value. I appreciate all of your help. I am very new to this and needed to create a complex program right off of the bat.
Walter Roberson
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
Evan Mossel le 26 Juil 2018
Modifié(e) : Evan Mossel le 26 Juil 2018
I changed the xdata/ydata to Xdata/Ydata but this did not resolve the issue.
Update: I misunderstood. This fixed the issue

Connectez-vous pour commenter.

Réponses (1)

Krithika A
Krithika A le 24 Juil 2018

0 votes

You use min in your code. min is already a matlab function

5 commentaires

Evan Mossel
Evan Mossel le 24 Juil 2018
I did notice that an fix that, but it gave the same error.
Krithika A
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
Evan Mossel le 24 Juil 2018
So I should look for the variable as max/min may only be able to handle positive integers, correct?
Krithika A
Krithika A le 24 Juil 2018
Not specifically for min/max. Min/max can handle non-integers. It would be another function.
Evan Mossel
Evan Mossel le 24 Juil 2018
I believe it is the 'find' function. I will try to work it out.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Modifié(e) :

le 26 Juil 2018

Community Treasure Hunt

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

Start Hunting!

Translated by