readtable working differently for different files

조회 수: 4 (최근 30일)
Louise Wilson
Louise Wilson 2021년 6월 8일
댓글: Louise Wilson 2021년 6월 8일
I have a bunch of spreadsheets which all have the same format. However, when I read them in, they appear differently...
folder=('H:\SoundTrap\Boats\Manual Vessel Detections');
d=dir(fullfile(folder,'*.csv'));
site=('Goat');
for i=1:height(d)
idx=contains(d(i).name,site);
if idx==1
out{i}=readtable(fullfile(folder,d(i).name));
end
end
For some reason, one of the files is read using a different delimeter? So the columns are incorrectly split. How can I rectify this? I have tried detect import options and 'Format' '%s%s%s' to no avail. Perhaps I am using these incorrectly though. The desired format is the one below:
  댓글 수: 3
Stephen23
Stephen23 2021년 6월 8일
@Louise Wilson: please upload two or more sample flles (ones which import differently) by clicking the paperclip button.
Louise Wilson
Louise Wilson 2021년 6월 8일
Thank you both, sorry for failing to do this in the first instance! I have attached two .csv files here. 001 is the file which is currently loading in as I would like it to.

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 8일
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/645435/001_GoatIsland_5100_manualVesselDetections.csv', 'delimiter', ',');
T1(1:10,:)
ans = 10×2 table
Filename BoatPresence ______________________________ ____________ {'5100.190610124350_2min.png'} 1 {'5100.190610152350_2min.png'} 1 {'5100.190610170350_2min.png'} 0 {'5100.190610182350_2min.png'} 1 {'5100.190610205350_2min.png'} 1 {'5100.190610214350_2min.png'} 1 {'5100.190610230350_2min.png'} 1 {'5100.190611003350_2min.png'} 1 {'5100.190611030350_2min.png'} 1 {'5100.190611041350_2min.png'} 0
T2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/645440/009_GoatIsland_5100_manualVesselDetections.csv', 'delimiter', ',');
T2(1:10,:)
ans = 10×3 table
Filename BoatPresence Var3 ______________________________ ____________ __________ {'5100.200316151440_2min.png'} 0 {0×0 char} {'5100.200316165440_2min.png'} 0 {0×0 char} {'5100.200316190440_2min.png'} 0 {0×0 char} {'5100.200316195440_2min.png'} 0 {0×0 char} {'5100.200316212440_2min.png'} 0 {0×0 char} {'5100.200317001441_2min.png'} 1 {0×0 char} {'5100.200317004441_2min.png'} 0 {0×0 char} {'5100.200317025441_2min.png'} 0 {0×0 char} {'5100.200317041441_2min.png'} 0 {0×0 char} {'5100.200317071441_2min.png'} 0 {0×0 char}
The files are different: the second one has an extra empty field, and that throws off MATLAB into thinking maybe the delimiter is underscore.
  댓글 수: 1
Louise Wilson
Louise Wilson 2021년 6월 8일
Thanks so much Walter! This fixes my problem and also helps me to understand why this happened. I was not aware there was a third empty column.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by