How to prevent readtable excluding rows with "NA"?

1 visualización (últimos 30 días)
BC
BC el 25 de Mayo de 2021
Editada: BC el 25 de Mayo de 2021
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.

Respuesta aceptada

Star Strider
Star Strider el 25 de Mayo de 2021
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 comentarios
BC
BC el 25 de Mayo de 2021
Editada: BC el 25 de Mayo de 2021
Thanks for another useful tip, makes things a bit tidier if using that approach!
Star Strider
Star Strider el 25 de Mayo de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by