Sorting data based on time

9 views (last 30 days)
James on 6 Mar 2020
Commented: Ameer Hamza on 9 Mar 2020
I have multiple csv files and each looks like this.
First column is an ID and the second one is the time (yyyymmddhhmmss). Here date does not change (yymmdd) but the hours changes. Is it possible to create seperate csv (or txt) files with a time limit?
For example, for one ID, could following files be created?
  • data raws between 00hr and 03hr
  • data raws between 03hr and 06hr
  • data raws between 06hr and 09hr etc.
For instance, if there are no data for a given time period, it either creates no csv file or creates an empty csv file.
Again, I'm trying to explore more with this approach. Any suggestion is appreciated!
Ameer Hamza
Ameer Hamza on 6 Mar 2020
Hi James, please check the code in the answer below.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 6 Mar 2020
Edited: Ameer Hamza on 6 Mar 2020
You can use readmatrix and writematrix functions to read and write to CSV files. The following code will create partitions of data and create several CSV files.
If you are using R2018b and earlier, you can use the commented lines.
data = int64(readmatrix('Sample.csv'));
% data = int64(csvread('Sample.csv')); % for R2018b and earlier
time = data(:,2);
% remove empty rows
data = data(time>0, :);
time = time(time>0);
time = mod(time, 1000000); % yyyymmdd are not important so discard them
current_time = 60000; % time in hhmmss format
while size(data, 1) > 0
next_time = current_time + 30000; % 30000 represent 3 hours
index = time < (current_time + 30000);
partial_data = data(index, :);
writematrix(partial_data, ...
['data-' num2str(current_time/10000) '-' num2str(next_time/10000) '.csv']);
% dlmwrite(['data-' num2str(current_time/10000) '-' num2str(next_time/10000) '.csv'], ...
% partial_data, 'precision', '%i'); % for R2018b and earlier
data(index,:) = [];
time(index,:) = [];
current_time = current_time + 30000;
Ameer Hamza
Ameer Hamza on 9 Mar 2020
Glad to be of help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!