How to prevent readtable excluding rows with "NA"?

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.

 Accepted Answer

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 Comments

Thanks for the help, this works!
As always, my pleasure!
If you only need to read the first column:
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/629630/exampletxt.txt';
T1 = readtable(filename,"Format","%s%*s%*s%*s%*s%*s%*s%*s")
T1 = 7×1 table
Image ________________ {'IMG_0560.jpg'} {'IMG_0561.jpg'} {'IMG_0562.jpg'} {'IMG_0565.jpg'} {'IMG_0566.jpg'} {'IMG_0567.jpg'} {'IMG_0568.jpg'}
The ‘*’ between the ‘%’ and the format descriptor omits reading that field.
BC
BC on 25 May 2021
Edited: BC on 25 May 2021
Thanks for another useful tip, makes things a bit tidier if using that approach!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2020b

Asked:

BC
on 25 May 2021

Edited:

BC
on 25 May 2021

Community Treasure Hunt

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

Start Hunting!