MATLAB Answers

Why my vars become to NaN?

22 views (last 30 days)
Tony Castillo
Tony Castillo on 18 Dec 2020
Commented: Tony Castillo on 18 Dec 2020
Dear Coders,
I have been testing the following code section by section and at first glance it worked properly, but when I have tested the code to plot only one curve per month, so that twelve curves, the vars ME2 and ME3 accumulate NaN values. Why this problem arises?
To add more information, Matlab return me this message
"REE_perfiles2020
Array indices must be positive integers or logical values.
Error in REE_perfiles2020 (line 174)
plot([ME(1i)])"
REE=xlsread('REE_perfiles2020.xlsx',1,'a3:h8786');
REE(744); %%%%V
NDias=REE(:,1); %%%%%
%%%%
aP=REE(:,4); %
ai=reshape(aP,[24, 366]); %%%%%
%
for NDias=1:length(NDias)
%
if NDias==1
[row1, col]=find(NDias==1);
dEne=length(row1)/24; %%%
E=ai(:,(1:31));
ME=mean(E,2);
elseif NDias==2
[row2, col]=find(NDias==2);
dFeb=length(row2)/24;
ld2=row2/24;
logFeb=max(ld2)-dFeb;
E2=ai(:,((max(ld2)-dFeb)+1:max(ld2)));
ME2=mean(E2,2);
elseif NDias==3
[row3, col]=find(NDias==3);
dMar=length(row3)/24;
ld3=row3/24;
logMar=max(ld3)-dMar;
E3=ai(:,((round(ld3)-dMar)+1:max(ld3)));
ME3=mean(E3,2);
elseif NDias==4
[row4, col]=find(NDias==4);
dAbr=length(row4)/24;
ld4=row4/24;
logAbr=max(ld4)-dAbr;
E4=ai(:,(round(logAbr):max(ld4)));
ME4=mean(E4,2);
elseif NDias==5
[row5, col]=find(NDias==5);
dMay=length(row5)/24;
ld5=row5/24;
logMay=max(ld5)-dMay;
E5=ai(:,((round(ld5)-dMay)+1:max(ld5)));
ME5=mean(E5,2);
elseif NDias==6
[row6, col]=find(NDias==6);
dJun=length(row6)/24;
ld6=row6/24;
logJun=max(ld6)-dJun;
E6=ai(:,((round(ld6)-dJun)+1:max(ld6)));
ME6=mean(E6,2);
elseif NDias==7
[row7, col]=find(NDias==7);
dJul=length(row7)/24;
ld7=row7/24;
logJul=max(ld7)-dJul;
E7=ai(:,((round(ld7)-dJul)+1:max(ld7)));
ME7=mean(E7,2);
elseif NDias==8
[row8, col]=find(NDias==8);
dAgo=length(row8)/24;
ld8=row8/24;
logAgo=max(ld8)-dAgo;
E8=ai(:,((round(ld8)-dAgo)+1:max(ld8)));
ME8=mean(E8,2);
elseif NDias==9
[row9, col]=find(NDias==9);
dSep=length(row9)/24;
ld9=row9/24;
logSep=max(ld9)-dSep;
E9=ai(:,((round(ld9)-dSep)+1:max(ld9)));
ME9=mean(E9,2);
elseif NDias==10
[row10, col]=find(NDias==10);
dOct=round(length(row10)/24);
ld10=row10/24;
logOct=max(ld10)-dOct;
E10=ai(:,(((round(logOct)-dOct)+1:max(ld10))));
ME10=mean(E10,2);
elseif NDias==11
[row11, col]=find(NDias==11);
dNov=length(row11)/24;
ld11=row11/24;
logNov=max(ld11)-dNov;
E11=ai(:,(((round(logNov)-dNov)+1:max(ld11))));
ME11=mean(E11,2);
else
[row12, col]=find(NDias==12); %%%l
dDic=length(row12)/24; %%%%
ld=row12/24;
logDic=max(ld)-dDic;
E12=ai(:,((max(ld)-dDic)+1:max(ld)));
ME12=mean(E12,2);
end
plot(ME(1i))
hold on
end

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 18 Dec 2020
Edited: KALYAN ACHARJYA on 18 Dec 2020
Here
plot(ME(1i))
M(1i) what does it means? 1i is a complex number for MATLAB
>> 1i
ans =
0.0000 + 1.0000i
MATLAB allows positive integer indexing only. More. Before plot ME in each iteration, ensure that ME return as a vector. Note: There is no ME variable in elseif condition.It seems that the code needs some modification.
  1 Comment
Tony Castillo
Tony Castillo on 18 Dec 2020
I think I undertood your recommendation, but I still has some bug here in my new code, becuase of this error:
Index in position 2 is invalid. Array indices must be positive integers or logical
values.
Error in REE_perfiles2020 (line 48)
E4=ai(:,(round(logAbr):max(ld4)));
%%%%%carg
REE=xlsread('REE_perfiles2020.xlsx',1,'a3:h8786');
REE(744); %%%%Ver
NDias=REE(:,1); %%%%%
aP=REE(:,4); %%%%Ext
ai=reshape(aP,[24, 366]); %%%
ME_=zeros(24,12);
%
for NDias=1:length(NDias)
%
%%%%se cargan los perfiles de consum
if NDias==1
[row1, col]=find(NDias==1); %
dEne=length(row1)/24; %
E=ai(:,(1:31));
ME=mean(E,2);
elseif NDias==2
[row2, col]=find(NDias==2);
dFeb=length(row2)/24;
ld2=row2/24;
logFeb=max(ld2)-dFeb;
E2=ai(:,((max(ld2)-dFeb)+1:max(ld2)));
ME=mean(E2,2);
elseif NDias==3
[row3, col]=find(NDias==3);
dMar=length(row3)/24;
ld3=row3/24;
logMar=max(ld3)-dMar;
E3=ai(:,((round(ld3)-dMar)+1:max(ld3)));
ME=mean(E3,2);
elseif NDias==4
[row4, col]=find(NDias==4);
dAbr=length(row4)/24;
ld4=row4/24;
logAbr=max(ld4)-dAbr;
E4=ai(:,(round(logAbr):max(ld4)));
ME=mean(E4,2);
elseif NDias==5
[row5, col]=find(NDias==5);
dMay=length(row5)/24;
ld5=row5/24;
logMay=max(ld5)-dMay;
E5=ai(:,((round(ld5)-dMay)+1:max(ld5)));
ME=mean(E5,2);
elseif NDias==6
[row6, col]=find(NDias==6);
dJun=length(row6)/24;
ld6=row6/24;
logJun=max(ld6)-dJun;
E6=ai(:,((round(ld6)-dJun)+1:max(ld6)));
ME=mean(E6,2);
elseif NDias==7
[row7, col]=find(NDias==7);
dJul=length(row7)/24;
ld7=row7/24;
logJul=max(ld7)-dJul;
E7=ai(:,((round(ld7)-dJul)+1:max(ld7)));
ME=mean(E7,2);
elseif NDias==8
[row8, col]=find(NDias==8);
dAgo=length(row8)/24;
ld8=row8/24;
logAgo=max(ld8)-dAgo;
E8=ai(:,((round(ld8)-dAgo)+1:max(ld8)));
ME=mean(E8,2);
elseif NDias==9
[row9, col]=find(NDias==9);
dSep=length(row9)/24;
ld9=row9/24;
logSep=max(ld9)-dSep;
E9=ai(:,((round(ld9)-dSep)+1:max(ld9)));
ME=mean(E9,2);
elseif NDias==10
[row10, col]=find(NDias==10);
dOct=round(length(row10)/24);
ld10=row10/24;
logOct=max(ld10)-dOct;
E10=ai(:,(((round(logOct)-dOct)+1:max(ld10))));
ME=mean(E10,2);
elseif NDias==11
[row11, col]=find(NDias==11);
dNov=length(row11)/24;
ld11=row11/24;
logNov=max(ld11)-dNov;
E11=ai(:,(((round(logNov)-dNov)+1:max(ld11))));
ME=mean(E11,2);
%
else
[row12, col]=find(NDias==12); %
dDic=length(row12)/24; %%%%d
ld=row12/24;
logDic=max(ld)-dDic;
E12=ai(:,((max(ld)-dDic)+1:max(ld)));
ME=mean(E12,2);
end
ME_(NDias)=NDias;
plot(ME(NDias))
hold on
end

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!