MATLAB Answers


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

Asked by Andrea Del Rosario on 10 Jan 2019
Latest activity Commented on by SHUBHAM GUPTA on 10 Jan 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
% -- for loop for NaN row deletion
[number, string, everything]=xlsread('writtenData.xlsx');
for col=1:1:size(everything,2) %to length of column
I have also attached the (.csv) file im working on here.

  1 Comment

Sign in to comment.

2 Answers

Answer by SHUBHAM GUPTA on 10 Jan 2019

Instead of finding 0 column wise, you can find NaN using 'isnan' function. Try this :
I hope this helps.


Show 1 older comment
Everyone has the built-in function isnan. What does this say, at the command line:
>> which -all isnan
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
Then it should not say that. Now, put a breakpoint at line 73:
Error in attempt4 (line 73)
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.

Sign in to comment.

Answer by Image Analyst
on 10 Jan 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 Comment

I think Andrea covered the row difference between 'everything' and 'number' by increasing 'rind' by '+1' in the following line
Let me know if I am missing out something, cheers.

Sign in to comment.