I am creating a date file using the following script, i want it to print DATES[] in the file my_dates.txt but it's only printing one date which is 2.0171e+09, that is when i type A(1,1)...i pleading for help?
조회 수: 1 (최근 30일)
이전 댓글 표시
for iy = 2005:2017
yr = num2str(iy);
for im = 1:12
if im < 10
mn = sprintf('0%s',num2str(im));
else
mn = num2str(im);
end
switch im
case {1 3 5 7 8 10 12}
M_END = 31;
case {4 6 9 11}
M_END = 30;
otherwise
%----------------------------------------------------------
% Check whether this is a leap year or not and then assign
% the appropriate month end.
%----------------------------------------------------------
ny = str2num(yr);
ly = isequal(floor(ny/4)*4,ny);
if ly == 1
M_END = 29;
else
M_END = 28;
end
end
for id = 1:M_END
if id < 10
dy = sprintf('0%s',num2str(id));
else
dy = num2str(id);
end
dt = strcat(yr,mn,dy); ndt = str2num(dt);
for ih = 0:6:18
if ih < 10
hr = sprintf('0%s',num2str(ih));
else
hr = num2str(ih);
end
DATES=[];
Date_STR= strcat(dt,hr);
Date_num= str2num(Date_STR);
end
end
end
end
DATES=vertcat(DATES,[Date_num]);
fn=sprintf('my_dates.txt');
fid=fopen(fn,'w');
fprintf(fid,'%10.2f\n',DATES');
fclose(fid);
댓글 수: 0
채택된 답변
Are Mjaavatten
2018년 7월 15일
The problem with your file is that you overwrite your Date_num value whenever you calculate a new one. Your DATA variable has no function inside the loop, After the loops, DATA will only replicate Date_num, and is thus superfluous.
Your approach is fine as a Matlab exercise, but for serious work I recommend that you type 'doc datetime' and learn about this and other built-in time and date functions.
Here is version of your code that will work and hopefully do what you want:
Date_num = [];
for iy = 2005:2017
yr = num2str(iy);
for im = 1:12
if im < 10
mn = sprintf('0%s',num2str(im));
else
mn = num2str(im);
end
switch im
case {1 3 5 7 8 10 12}
M_END = 31;
case {4 6 9 11}
M_END = 30;
otherwise
%----------------------------------------------------------
% Check whether this is a leap year or not and then assign
% the appropriate month end.
%----------------------------------------------------------
ny = str2num(yr);
ly = isequal(floor(ny/4)*4,ny);
if ly == 1
M_END = 29;
else
M_END = 28;
end
end
for id = 1:M_END
if id < 10
dy = sprintf('0%s',num2str(id));
else
dy = num2str(id);
end
dt = strcat(yr,mn,dy); ndt = str2num(dt);
for ih = 0:6:18
if ih < 10
hr = sprintf('0%s',num2str(ih));
else
hr = num2str(ih);
end
% DATES=[];
Date_STR= strcat(dt,hr);
Date_num= [Date_num;str2num(Date_STR(end,:))];
end
end
end
end
% DATES=vertcat(DATES,[Date_num]);
fn=sprintf('my_dates.txt');
fid=fopen(fn,'w');
fprintf(fid,'%10.2f\n',Date_num);
fclose(fid);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multiobjective Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!