How to find NaN value then delete whole row using for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
답변 (2개)
Shubham Gupta
2019년 1월 10일
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
Image Analyst
2019년 1월 10일
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
2019년 1월 10일
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
Shubham Gupta
2019년 1월 10일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!