Error in script Subscript indices must either be real positive integers or logicals.

1 回表示 (過去 30 日間)
Kostas
Kostas 2014 年 11 月 7 日
コメント済み: Matt J 2014 年 11 月 7 日
I am trying to calculate the mean, max and min values from an array under some condition and at some specific case i get a very "strange" error
Subscript indices must either be real positive integers or logicals.
Error in MeanHourlyTOC (line 31)
minTOC = min(dummy);
since i am able to calculate the max and mean values and this error is referred just in the min value. My code is as follow
l=0;
for i=1982:2012
for j=1:12
for k=1:24
idx=find(Yr==i&M==j&T==k);
dummy=TOC(idx);
dummy(isnan(dummy))=[];
if ~isempty(dummy)
l=l+1;
mTOC = mean(dummy);
sTOC = std(dummy);
maxTOC = max(dummy);
minTOC = min(dummy);
dy = decyear(i,j,15,k,0,0);
Output.hTOCMonthlyMean(l,:) = [dy i j k mTOC sTOC maxTOC minTOC length(dummy)];
end
end
end
end
and the moment the script terminates the array dummy is
>> dummy
dummy =
368.8352
334.5709
341.9396
I would appreciate any help to resolve this issue

採用された回答

Matt J
Matt J 2014 年 11 月 7 日
編集済み: Matt J 2014 年 11 月 7 日
Put
clear min
just prior to the for-loop. Preferably also, avoid using "min" as the name of one of your variables.

その他の回答 (1 件)

Adam
Adam 2014 年 11 月 7 日
Do you have a variable named min further up the code?
The error message is typical of such a scenario where the code thinks that the instruction
minTOC = min(dummy);
is trying to index into a variable called 'min' using whatever is in dummy as indices rather than call the 'min' function.
Variables always take precedence over functions and will hide them if you reuse the name of a function as a variable.
which min
at the appropriate point will tell you whether you have such a variable. If you don't it will point you to where the builtin function is, if you have a variable it will just say:
min is a variable.
  3 件のコメント
Adam
Adam 2014 年 11 月 7 日
That is interesting.
clear min
would normally clear a variable called min and thus work in that way. It would also clear any other functions and other detritus in its cache called min, but that is quite surprising that you got such behaviour with no variable called min.
Matt J
Matt J 2014 年 11 月 7 日
I am skeptical that Kostas did not have a variable called 'min'.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by