How to seperate column correctly using readtable?

조회 수: 15 (최근 30일)
JamJan
JamJan 2021년 6월 3일
편집: Stephen23 2021년 6월 10일
Hi,
I have been using readtable for quite some while, but for some reason I started running into problems a few days ago. Attached find an example of a .csv file. I would like to split the values from:
FLIP001,111111.1234567890,"2001-10-29 01:01:00","2001-10-29 01:01:00",7
to a table consisting of:
Var1; Var2; Var3; Var4; Var5
FLIP001; 111111.1234567890; "2001-10-29 01:01:00"; "2001-10-29 01:01:00"; 7
(Semicolons are seperating the columns here)
For some reason I do not manage to use the delimiter correctly. Can someone help me to split these variables correctly using, for instance, readtable or any other way?
Thank you!
  댓글 수: 2
Rik
Rik 2021년 6월 10일
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Stephen23
Stephen23 2021년 6월 10일
편집: Stephen23 2021년 6월 10일
Original question by JamJam retrieved from Bing Cache (the CSV file could not be retrieved):
How to seperate column correctly using readtable?
I have been using readtable for quite some while, but for some reason I started running into problems a few days ago. Attached find an example of a .csv file. I would like to split the values from:
FLIP001,111111.1234567890,"2001-10-29 01:01:00","2001-10-29 01:01:00",7
to a table consisting of:
Var1; Var2; Var3; Var4; Var5
FLIP001; 111111.1234567890; "2001-10-29 01:01:00"; "2001-10-29 01:01:00"; 7
(Semicolons are seperating the columns here)
For some reason I do not manage to use the delimiter correctly. Can someone help me to split these variables correctly using, for instance, readtable or any other way?
Thank you!

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

채택된 답변

Star Strider
Star Strider 2021년 6월 3일
This appears to work —
C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/640960/FakeCsv.csv');
CS1 = cellfun(@(x)strsplit(x,','), C1, 'Unif',0);
cellrows = cellfun(@(x)size(x,2),CS1);
CS2 = reshape([CS1{:}], cellrows(1), []).';
T1 = cell2table(CS2,'VariableNames',compose('Var%d',1:size(CS2,2)))
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ___________ _____________________ _________________________ _________________________ _______ {'FLIP001'} {'111111.1234567890'} {'"2001-10-29 01:01:00"'} {'"2001-10-29 01:01:00"'} {'7' } {'FLIP001'} {'111111.1234567890'} {'"2001-10-29 01:01:00"'} {'"2001-10-29 01:01:00"'} {'8' } {'FLIP001'} {'111111.1234567890'} {'"2001-10-29 01:01:00"'} {'"2001-10-29 01:01:00"'} {'16' } {'FLIP001'} {'111111.1234567890'} {'"2001-10-29 01:01:00"'} {'"2001-10-29 01:01:00"'} {'128'} {'FLIP001'} {'111111.1234567890'} {'"2001-10-29 01:01:00"'} {'"2001-10-29 01:01:00"'} {'7' }
Completing this with the conversions:
T1.Var2 = str2double(T1.Var2);
T1.Var3 = datetime(T1.Var3, 'InputFormat','"yyyy-MM-dd HH:mm:ss"');
T1.Var4 = datetime(T1.Var4, 'InputFormat','"yyyy-MM-dd HH:mm:ss"', 'InputFormat','"yyyy-MM-dd HH:mm:ss"');
T1.Var5 = str2double(T1.Var5)
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ___________ __________ ____________________ ____________________ ____ {'FLIP001'} 1.1111e+05 29-Oct-2001 01:01:00 29-Oct-2001 01:01:00 7 {'FLIP001'} 1.1111e+05 29-Oct-2001 01:01:00 29-Oct-2001 01:01:00 8 {'FLIP001'} 1.1111e+05 29-Oct-2001 01:01:00 29-Oct-2001 01:01:00 16 {'FLIP001'} 1.1111e+05 29-Oct-2001 01:01:00 29-Oct-2001 01:01:00 128 {'FLIP001'} 1.1111e+05 29-Oct-2001 01:01:00 29-Oct-2001 01:01:00 7
There does not appear to be a more straightforward way to parse that. The detectImportOptions options may work, however I have found those to be difficult to get right. so I went for a more direct approach.
.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by