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
Aquatris
2018년 7월 23일
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
2018년 7월 23일
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
2018년 7월 23일
편집: Walter Roberson
2018년 7월 24일
Walter Roberson
2018년 7월 23일
Examine your overall code more closely. You probably accidentally created a variable named "max".
Evan Mossel
2018년 7월 24일
Walter Roberson
2018년 7월 24일
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
2018년 7월 24일
편집: Evan Mossel
2018년 7월 24일
Walter Roberson
2018년 7월 24일
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
2018년 7월 24일
편집: Evan Mossel
2018년 7월 24일
Walter Roberson
2018년 7월 24일
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
2018년 7월 24일
편집: Evan Mossel
2018년 7월 24일
Walter Roberson
2018년 7월 24일
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
2018년 7월 24일
편집: Evan Mossel
2018년 7월 24일
Walter Roberson
2018년 7월 24일
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
2018년 7월 26일
편집: Evan Mossel
2018년 7월 26일
답변 (1개)
Krithika A
2018년 7월 24일
0 개 추천
You use min in your code. min is already a matlab function
댓글 수: 5
Evan Mossel
2018년 7월 24일
Krithika A
2018년 7월 24일
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
2018년 7월 24일
Krithika A
2018년 7월 24일
Not specifically for min/max. Min/max can handle non-integers. It would be another function.
Evan Mossel
2018년 7월 24일
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!