How to separate each day of this file?

1 view (last 30 days)
Hello,
Kindly find the attached text file. It contains data for the entire January 2014.
Each row represents a data record per hour so that it starts from hour=0 (the 3rd column) to hour=23 and then starts again from hour=0 for the next day, and so on.
I need to loop over the rows and save each day (24 hours; rows) separately in a text file (i.e. d1, d2, ..., d31).
Could you please tell me how to do that?
Thanks in advance.

Accepted Answer

madhan ravi
madhan ravi on 17 Jan 2019
Edited: madhan ravi on 17 Jan 2019
T=load('Jan.txt');
TT=mat2cell(T,repmat(24,numel(unique(T(:,2))),1));
for i = 1:numel(unique(T(:,2)))
dlmwrite(sprintf('file%d',i),TT{i},'precision','%.2f','delimiter',' ') % saves each day in a separate file , filenames will be like file1.txt ... and so on till the end
end
  4 Comments
Mohamed Nedal
Mohamed Nedal on 17 Jan 2019
Thanks a lot. :)
Yes, I've accepted it.
Mohamed Nedal
Mohamed Nedal on 9 Jan 2020
Hi Ravi, i hope you're fine.
Please I would like to ask you one more question.
Kindly find the attached file. It's similar to the previous file (Jan.txt), but it's for the whole year of 2018 (includes all months).
I need to do the same thing as you did, but to separate each month in a separate file and then I will apply your previous code on each one to separate each month into days.
Could you tell me how to separate the whole year into months?
The issue is some months have 30 days, some have 31 days, and April sometimes has 28 days or 29 days.
I really appreciate your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!