Split a column into two based on a delimiter
45 views (last 30 days)
Show older comments
Hi eveyone,
I have a table with one column and several rows of time stamps:
1 6:50.4
2 6:55.9
3 6:50.8
4 6:58.6
5 7:01.8
How do I split the table into 2 columns based on the first space separator ?
Thanks for your time!
12 Comments
dpb
on 7 Feb 2021
Show us the file from which importing -- a pdf file is just text so may be best can do is all internal, but I still would not incorporate the column header into the data; it isn't data, so don't treat it as if were.
If the reading returns a string array like you showed above, as Chris says, then to split() it, you need to just forget the first row for the data and use the first row for the table variable name.
But need to know more about the timestamps -- are those durations or 24hr times-of-day? There's an inconsistency in the first entry of "6:50:4" and the rest that are (it appears) mm:ss.S but the first has what appears to be HH:mm:ss format.
Is the first an actual time while the rest are durations or what?
Accepted Answer
More Answers (1)
Cris LaPierre
on 7 Feb 2021
Edited: Cris LaPierre
on 7 Feb 2021
Your solution will not work if there are triple spaces. Also, your numbers get recorded as strings, which will make it hard to use the values for any computations.
Not sure what the end goal is, but here's an approach that creates a result I would want. Not sure what the timestamp format is so I assumed mm:ss.S
a=[ "Machine"
"1 6:50:4"
"2 6:55.9"
"3 6:50.8"
"4 6:58.6"
"5 7:01.8"
"6 6:10.1"
"7 7:38.0"
"8 6:33.7"
"9 10:52.6"
"10 14:20.7"
"11 11:30.8"
"12 12:13.9"
"13 11:30.9"
"14 11:59.5"
"15 24:13.7"
"16 22:16.2"
"17 6:35.9"
"18 10:35.9"
"19 11:36.2"];
% Looks scary, but applys strsplit to each element of the array ignoring multiple delimiters.
% It then converts the result to a double
data = arrayfun(@(a)str2double(strsplit(a,[" ",":","."],"CollapseDelimiters",true)),a(2:end),"UniformOutput",false);
% convert cell array to a matrix
data = cell2mat(data);
% convert numbers back to time
timestamp = duration(0,data(:,2),data(:,3),data(:,4)*100,"Format","mm:ss.S");
% Convert to a table. Use 'Machine" as the variable name
Data = table(data(:,1),timestamp,'VariableNames',[a(1),"timestamp"])
3 Comments
Cris LaPierre
on 8 Feb 2021
Edited: Cris LaPierre
on 8 Feb 2021
Datetime will include a date. Since you hadn't indicated there was one, I opted to use a duration. It has the same benefits of a datetime when it comes to computation without having a date included.
See Also
Categories
Find more on Language Support 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!