Why array only saves last run of results from nested FOR Loop?
Show older comments
Hello guys, I have been seeking for a solution for this, but I can't find it. I created a for loops and store values for each loops in array. However, the only results from last run are save to array. Could you please explain me why and how to solve it? Here is my code, it is about create a file name with a format of 'HT-yyyy-mm-dd-hh-mi.dat'. yyyy refers as year, mm is month, dd is day, hh is hour, and mi is minute. 'My guess is that the array I preallocated is redefined again in each loops, so only the last run is saved. But how to fix it I have no idea.
Date_matrix=strings(No_of_file_1D,1,numdays);
for ii=Num_date_i:Num_date_j
folder_ii=datestr(ii,'yyyy-mm-dd');
[yy,mm,dd] = ymd(datetime(folder_ii, 'ConvertFrom', 'datenum'));
numdays_i=(ii - Num_date_i)+1;
for hh=0:1:24
hh_i=hh;
for mi=10:10:60
mi_i=mi;
index_save=(numdays_i)*No_of_file_1D+hh*(60/Time_interval)+(mi)/10;
if mi==60
mi_i=0;
hh_i=hh+1;
else
end
if hh==24
hh_i=00;
dd=dd+1;
else
end
file_name_ii=sprintf('%s%04d%c%02d%c%02d%c%02d%c%02d%s','HT-',yy,'-',mm,'-',...
dd,'-',hh_i,'-',mi_i,'.dat');
Date_matrix(index_save,1,numdays_i)=file_name_ii;
end
end
end
Thank you in advance
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!