How to find NaN value then delete whole row using for loop

10 visualizaciones (últimos 30 días)
Andrea Del Rosario
Andrea Del Rosario el 10 de En. de 2019
Comentada: Shubham Gupta el 10 de En. de 2019
Hi there,
I want to use a for loop to find all NaN cell to my new saved file 'writtenData.xlsx' and then delete the entire row. I am aware of using 'rmmissing' to do this however, I've got an older version of matlab (R2016a) and hence I want to see if there is a way to manipulate the forloop I have below. If anyone can please help me manipulate the code I have so it deletes the rows that contains NaN values. That would be greatly appreciated. Please Help :(
%filter the columns using readtable and .csv default file
PCBAllDataSet = readtable('testfile6.csv');
%imports the only columns needed
PCBRequiredCol = PCBAllDataSet(:,{'PcbID','Verify12VInput','VerifyCurrentDraw','MeasureG9Gate','MeasureG18Gate','MeasureU21Gate','SetG9Power','SetG18Power','SetU21Power','FinalSystemCurrent','CheckG9BandPower','CheckG18BandPower','CheckU21BandPower','CheckR3OutputPower','CheckR4OutputPower'});
%writing/creating new csv file to .xlsx after a readtable function - gets saved to same directory
writetable(PCBRequiredCol,'writtenData.xlsx');
% -- for loop for NaN row deletion
[number, string, everything]=xlsread('writtenData.xlsx');
for col=1:1:size(everything,2) %to length of column
rind=find(number(:,col)==0);
everything(rind+1,:)=[];
end
I have also attached the (.csv) file im working on here.

Respuestas (2)

Shubham Gupta
Shubham Gupta el 10 de En. de 2019
Instead of finding 0 column wise, you can find NaN using 'isnan' function. Try this :
rind=find(isnan(:,col));
everything(rind+1,:)=[];
I hope this helps.
  4 comentarios
Andrea Del Rosario
Andrea Del Rosario el 10 de En. de 2019
Hi I got this,
which -all isnan
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@double/isnan) % double method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@uint8/isnan) % uint8 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@uint16/isnan) % uint16 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@uint32/isnan) % uint32 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@uint64/isnan) % uint64 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@int8/isnan) % int8 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@int16/isnan) % int16 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@int32/isnan) % int32 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@int64/isnan) % int64 method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@single/isnan) % single method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@char/isnan) % char method
built-in (/opt/mlsedu/matlab/R2018b/toolbox/matlab/elmat/@logical/isnan) % logical method
/opt/mlsedu/matlab/R2018b/toolbox/matlab/timefun/@duration/isnan.m % duration method
isnan is a built-in method % connector.internal.LoggerLevel method
isnan is a built-in method % matlab.lang.OnOffSwitchState method
isnan is a built-in method % matlab.unittest.Verbosity method
/opt/mlsedu/matlab/R2018b/toolbox/matlab/timefun/@calendarDuration/isnan.m % calendarDuration method
/opt/mlsedu/matlab/R2018b/toolbox/ident/ident/@iddata/isnan.m % iddata method
/opt/mlsedu/matlab/R2018b/toolbox/ident/nlident/@idnlmodel/isnan.m % idnlmodel method
/opt/mlsedu/matlab/R2018b/toolbox/ident/nlident/@idnlgrey/isnan.m % idnlgrey method
/opt/mlsedu/matlab/R2018b/toolbox/distcomp/parallel/@codistributed/isnan.m % codistributed method
/opt/mlsedu/matlab/R2018b/toolbox/distcomp/gpu/@gpuArray/isnan.m % gpuArray method
/opt/mlsedu/matlab/R2018b/toolbox/symbolic/symbolic/@sym/isnan.m % sym method
Image Analyst
Image Analyst el 10 de En. de 2019
Then it should not say that. Now, put a breakpoint at line 73:
Error in attempt4 (line 73)
rind=find(isnan(:,col));
and before you execute that line, do the which statement in the command window again and see what it says. Then execute the line.
Also, see my Answer below for another possible error, once we solve this one.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 10 de En. de 2019
The indexes for number and everything are not synced up, so you can't use find() on number, then use the results to filter the same rows in everything!
  1 comentario
Shubham Gupta
Shubham Gupta el 10 de En. de 2019
I think Andrea covered the row difference between 'everything' and 'number' by increasing 'rind' by '+1' in the following line
everything(rind+1,:)=[];
Let me know if I am missing out something, cheers.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by