Split one CSV file in 4 parts
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Bob Thompson
2019년 5월 24일
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.
채택된 답변
Guillaume
2019년 5월 24일
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!