how to split table with different time values

Hello,
I have an attached table with time column (hh:mm:ss) and a data column. I'd like to make separate columns or tables according to the different time (minutes) and plot seperate figures for that.
I attached the ''csv'' file here. Can someone help me, pls?
a = readtable('tb.csv')
a = 675×2 table
time Var1 ________ ________ 14:46:13 -27.426 14:46:13 -12.221 14:46:13 15.665 14:46:13 -27.113 14:46:13 -12.416 14:46:13 -1.3964 14:46:13 16.638 14:46:14 -25.833 14:46:14 -10.794 14:46:14 -0.43471 14:46:14 17.373 14:46:14 -17.638 14:46:14 -7.1675 14:46:14 10.33 14:46:14 20.7 14:46:15 -26.28

2 comentarios

Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2022
Minutes meaning minute of the hour? or from a reference time?
uzzi
uzzi el 2 de Nov. de 2022
Editada: uzzi el 2 de Nov. de 2022
Minutes means by minutes of the hour. As shown in figure taken from the attached csv table, there are multiple seconds for 14 hour 46 minutes. I want to plot for 14 hour 46 minutes and 15 hour 02 minutes, etc. separately.

Iniciar sesión para comentar.

 Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2022
Editada: Dyuman Joshi el 2 de Nov. de 2022
Finding the indices where the minute starts/changes/ends -
a = readtable('tb.csv');
[~,~,~,h,m,~]=datevec(a.time);
y=unique([1 find(diff(m)~=0)'+1 numel(m)+1])
y = 1×8
1 150 236 397 503 526 658 676
You can convert the data in different individual elements like this -
z=mat2cell(a.Var1,diff(y),1)
z = 7×1 cell array
{149×1 double} { 86×1 double} {161×1 double} {106×1 double} { 23×1 double} {132×1 double} { 18×1 double}
%you can plot directly as well
for i=1:numel(y)-1
figure
arr=y(i):y(i+1)-1;
plot(a.time(arr),a.Var1(arr),'color', rand(1,3))
end

5 comentarios

uzzi
uzzi el 2 de Nov. de 2022
Dear Dyuman Josh,
Thank you very much for your answers. I really appreciate it.
The x axis should be time just like given table. Not some numerical values. Can you help me with this x axis thing?
It's okay. I figured out the x-axis thing.
If you don't mind, can you explain how you find the indexes (these 3 lines below) where time changes happens? I am new to Matlab and your explanation will be very helpful for me to understand.
[~,~,~,h,m,~]=datevec(a.time);
y=unique([1 find(diff(m)~=0)'+1 numel(m)+1])
z=mat2cell(a.Var1,diff(y),1)
Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2022
Yeah, sure.
So the 1st line extracts the hour and minute values of each element (though the hour value was not needed afterwards)
Then in the second line, to find the values where the minute value changes, the difference between minute values is calculated using diff. For same consecutive values, the difference will be zero, so we look for the indices where the difference is not zero, with the help of find. However, in this way there's a chance we miss the 1st and last element, if they are not different from the 2nd and 2nd last element respecitvely, so I add them.
If the values are repeated, unique will clear the duplicate values.
And in the third line, I made the partition of the data accordingly, into cell arrays, using the size of each minute's repetition.
uzzi
uzzi el 2 de Nov. de 2022
Thanks a ton, Dyuman Joshi. I understood it now. Your explanation is very clear.
I was trying this since morning and I couldn't crack it. You made my day :)
Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2022
You are welcome!
Glad to have helped.

Iniciar sesión para comentar.

Más respuestas (1)

Lei Hou
Lei Hou el 18 de Nov. de 2022
Hi,
Datevec is in discourage use now. You can use retime to group your data and do plot.
>> tt = readtimetable('tb.csv')
tt =
675×1 timetable
time Var1
________ _______
14:46:13 -27.426
14:46:13 -12.221
14:46:13 15.665
14:46:13 -27.113
14:46:13 -12.416
: :
15:58:02 2.4096
15:58:02 12.741
15:58:02 7.5679
15:58:02 11.63
15:58:02 6.6887
Display all 675 rows.
>> tt1 = retime(tt,"minutely",@(x){x})
tt1 =
73×1 timetable
time Var1
________ ______________
14:46:00 {149×1 double}
14:47:00 { 0×1 double}
14:48:00 { 0×1 double}
14:49:00 { 0×1 double}
14:50:00 { 0×1 double}
: :
15:54:00 { 0×1 double}
15:55:00 { 0×1 double}
15:56:00 { 0×1 double}
15:57:00 {132×1 double}
15:58:00 { 18×1 double}
Display all 73 rows.
>> tt2 = tt1(cellfun(@(x)~isequal(x,double.empty(0,1)),tt1.Var1),:)
tt2 =
7×1 timetable
time Var1
________ ______________
14:46:00 {149×1 double}
15:02:00 { 86×1 double}
15:27:00 {161×1 double}
15:35:00 {106×1 double}
15:48:00 { 23×1 double}
15:57:00 {132×1 double}
15:58:00 { 18×1 double}

1 comentario

uzzi
uzzi el 18 de Nov. de 2022
Thank you. This answer works well with this problem as well :)

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 2 de Nov. de 2022

Comentada:

el 18 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by