Split one CSV file in 4 parts

5 views (last 30 days)
Pierre
Pierre on 24 May 2019
Commented: Pierre on 27 May 2019
Hello,
I would like to know, how can I split a CSV file in 4 parts ? As you can see attached, there is a CSV file from an accelerometer plotted and I would like to split it in 4 parts. You can also find attached the program that I'm using to plot the file (test4.m).
First part: Between 0 to 12 seconds called "entering"
Second part: 12 to 118 seconds called "preparing"
Third part: 118 to 395 seconds called "drinking"
Fourth part: 395 to the end called "exiting"
The thing is that the time on x axis (in seconds) is converted from the timestamp of the accelerometer. (you can find the CSV used attached)
Save these parts in 4 different CSV files
Thank you for your help.
(MATLAB R2018b)
  2 Comments
Bob Thompson
Bob Thompson on 24 May 2019
I don't have the ability to look at the input files, sorry, but can you tell me if the number of rows for the two files is 1-1? Do you have one set of data for each time, and one time for each set of data?
If this is the case then I would suggest reading both files into matlab and just doing a split based on indexing. Something like this:
data = csvread('datafile.csv');
time = csvread('timefile.csv');
entering = data(time<=12);
preparing = data(time>12&time<=118);
drinking = data(time>118&time<=395);
exiting = data(time>395);
If you have time information in a datetime format, then the operation is much the same, you just need to convert it to datetime first.
Pierre
Pierre on 24 May 2019
Hello,
Thank you for your answer but it's not that I'm trying to do. The time and the data are in the same CSV file. First column is the time and the second, third and fourth are the data which are x, y and z accelereration.
The time in the CSV file is not in second, I'm converting it in my program attached.
Thank you

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 May 2019
Don't you have the documentation that explains how the timestamps are encoded? Using these would be more reliable than reconstructing the time.
Anyway, here is how I'd read and store the data:
opt = detectImportOptions('P9.1.csv', 'VariableNamesLine', 2); %Has to use detectImportOptions because of the complex header
drink = readtable('P9.1.csv', opt); %import as table. Much cleaner than csvread
drink = table2timetable(drink, 'SampleRate', 51.2); %Don't even have to bother calculating the time. Matlab does that for you with timetables
And then, to plot it, I'd use
stackedplot(drink(:, {'AccelerometerX', 'AccelerometerY', 'AccelerometerZ'}));
Or you can still use the old fashioned plot
plot(drink.Time, drink{:, 2:4})
Now, to categorise the rows according to time, it's trivial
timebins = seconds([0, 12, 118, 395, Inf]);
drink.Action = discretize(drink.Time, timebins, 'categorical', {'entering', 'preparing', 'drinking', 'exiting'});
drink = movevars(drink, 'Action', 'Before', 1) %make Action the first variable for easier visibility
If you really want to, you can then split this into 4 different timetables, but most likely it's a waste of time. You'd be better off working with the full table. There are plenty of functions that allow you to get statistics for each Action (such as groupsummary) for the whole timetable at once.
To do the splitting:
[group, action] = findgroups(drink.Action);
drinkperaction = splitapply(@(rows) {drink(rows, :)}, (1:height(drink))', group);
drinkperaction{i} is the portion of the timetable that corresponds to action{i}. You can easily save that to a file with writetimetable in a loop.
  1 Comment
Pierre
Pierre on 27 May 2019
Thank you for your answer, I will try this. There is no solution using my program ?
If anyone else has another solution, please let me know.
Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!