How to prevent readtable excluding rows with "NA"?

조회 수: 1 (최근 30일)
BC
BC 2021년 5월 25일
편집: BC 2021년 5월 25일
I have a textfile in this format (also attached) produced from an image processing loop, which when read in using readtable(), gives me the resulting table.
% Image X Y Area Circularity Extent Height Width
% IMG_0560.jpg NA NA NA NA NA NA NA
% IMG_0561.jpg NA NA NA NA NA NA NA
% IMG_0562.jpg NA NA NA NA NA NA NA
% IMG_0565.jpg 2388.9432 1028.7865 740 1.0291 0.83053 2736 3648
% IMG_0566.jpg 3020.4438 1152.3859 1433 0.99398 0.7788 2736 3648
% IMG_0567.jpg NA NA NA NA NA NA NA
% IMG_0568.jpg 3512.2915 878.88912 1434 1.0338 0.83275 2736 3648
readtable("C:\Desktop\exampletxt.txt");
% Image X Y Area Circularity Extent Height Width
% ________________ ______ ______ ____ ___________ _______ ______ _____
%
% {'IMG_0565.jpg'} 2388.9 1028.8 740 1.0291 0.83053 2736 3648
% {'IMG_0566.jpg'} 3020.4 1152.4 1433 0.99398 0.7788 2736 3648
% {'IMG_0567.jpg'} NaN NaN NaN NaN NaN NaN NaN
% {'IMG_0568.jpg'} 3512.3 878.89 1434 1.0338 0.83275 2736 3648
As you can see it excludes the first few rows that contain NAs, which I need to be included, as it has done correctly for the third row. Is there a way to do this with readtable? Ultimately, all I need is the first column, as I'm then using contain() to see if a given image name exists in this textfile.

채택된 답변

Star Strider
Star Strider 2021년 5월 25일
Try this —
C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/629630/exampletxt.txt');
VN = C1(1,:);
C1a = C1(2:end,:);
NAI = strcmp(C1(2:end,:), 'NA');
C1a(NAI) = {'NaN'};
T1 = cell2table(C1a, 'VariableNames',VN)
T1 = 7×8 table
Image X Y Area Circularity Extent Height Width ________________ ______________ ______________ ________ ___________ __________ ________ ________ {'IMG_0560.jpg'} {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'IMG_0561.jpg'} {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'IMG_0562.jpg'} {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'IMG_0565.jpg'} {[2.3889e+03]} {[1.0288e+03]} {[ 740]} {[1.0291]} {[0.8305]} {[2736]} {[3648]} {'IMG_0566.jpg'} {[3.0204e+03]} {[1.1524e+03]} {[1433]} {[0.9940]} {[0.7788]} {[2736]} {[3648]} {'IMG_0567.jpg'} {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'NaN' } {'IMG_0568.jpg'} {[3.5123e+03]} {[ 878.8891]} {[1434]} {[1.0338]} {[0.8327]} {[2736]} {[3648]}
.
  댓글 수: 4
BC
BC 2021년 5월 25일
편집: BC 2021년 5월 25일
Thanks for another useful tip, makes things a bit tidier if using that approach!
Star Strider
Star Strider 2021년 5월 25일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by