필터 지우기
필터 지우기

Split a column into two based on a delimiter

조회 수: 36 (최근 30일)
klb
klb 2021년 2월 6일
편집: Cris LaPierre 2021년 2월 8일
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
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?
klb
klb 2021년 2월 7일
편집: klb 2021년 2월 7일
"6:50:4" is typo. Should be "6:50.4" - it is correct in the original question. These are activation durations, hence not a 24 hour time of day.
Regarding the format, if you imagine pdf of several pages, with header and footer containing text and page numbers. So that is the preprocessing cleaning. The data is a rolling list of several machines/equipemt and timestamps i.e. Machine A , 20 timestamps.Next Machine B with 5 timestamps , Machine C with 30 timestamps, Machine D with no timestamps (meanig it wasnt used) and so on. The number of machines being monitored can vary and the number of timestamps vary too as they depend on usage. As I mentioend text headers are identifiers hence must be retained.

댓글을 달려면 로그인하십시오.

채택된 답변

klb
klb 2021년 2월 7일
편집: klb 2021년 2월 7일
Needed formatting. I chaged name to Machine A so now the word includes one space i.e. the delimiter as well. Where the spacing between variables is inconsistent (lines 14 and 15), I've rectified that too with the replace function.
Now everything has the same delimiter and the same number of delimiters.
a=["Machine A"
"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"]
a = replace(a," "," ")
timestamp = table(split(a));
timestamp = splitvars(timestamp, "Var1","NewVariableNames",["sl. no","timestamp"])

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 2월 7일
편집: Cris LaPierre 2021년 2월 7일
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"])
Data = 19x2 table
Machine timestamp _______ _________ 1 06:50.4 2 06:55.9 3 06:50.8 4 06:58.6 5 07:01.8 6 06:10.1 7 07:38.0 8 06: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
  댓글 수: 3
Cris LaPierre
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
klb 2021년 2월 8일
Data is timestamps only. No date, yet the datetime() worked out. Which is why I am curious that the split() rather than skip/do-nothing, returns an error when it doesnt find the delimiter. Will keep your approach in mind for future use. Thank you agin for your time!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by