Split a column into two based on a delimiter
이전 댓글 표시
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
dpb
2021년 2월 6일
split maybe?
Cris LaPierre
2021년 2월 6일
How are you importing your data? There may be an opportunity to load the data and avoid this problem completely.
klb
2021년 2월 6일
Cris LaPierre
2021년 2월 6일
Ok, then there is no better way I can think of.
klb
2021년 2월 6일
dpb
2021년 2월 7일
Blanks don't matter; split treats multiple spaces as one:
> split(cellstr(char('1 6:50.4','2 6:55.9','3 6:50.8','4 6:58.6','5 7:01.8')))
ans =
5×2 cell array
{'1'} {'6:50.4'}
{'2'} {'6:55.9'}
{'3'} {'6:50.8'}
{'4'} {'6:58.6'}
{'5'} {'7:01.8'}
>>
where removed the extraneous blank in first record; left in the rest.
Show code; attach actual dataset that has problem.
Cris LaPierre
2021년 2월 7일
You need to skip the first value of a, since that does not have a delimiter in it.
split(a(2:end)." ")
dpb
2021년 2월 7일
"is for a machine, hence the column title. "
Don't read the titile into the data array; if use a table, tell readtable to either skip a headerline or to use the first line as table variable names. If do that, then readtable can split the two columns on input for you automagically.
Using detectImportOptions first would probably take care of it all for you without other intervention.
klb
2021년 2월 7일
dpb
2021년 2월 7일
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?
채택된 답변
추가 답변 (1개)
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
Cris LaPierre
2021년 2월 8일
편집: Cris LaPierre
2021년 2월 8일
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.
klb
2021년 2월 8일
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
