How to find NaN value then delete whole row using for loop
1 view (last 30 days)
Show older comments
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.
Answers (2)
Shubham Gupta
on 10 Jan 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 Comments
Image Analyst
on 10 Jan 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.
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
Shubham Gupta
on 10 Jan 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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!