How can I remove NaN values from a matrix?

조회 수: 3,524 (최근 30일)
Brandon Walker
Brandon Walker 2015년 2월 18일
편집: Stephen23 2020년 8월 15일
My code so far is below. I have the code so that it skips the first 19 lines and starts at line 20. However, I need to remove the NaN values that are in my data like Columns = [10;0.04500;0;NaN;NaN] for example. The line I have to remove the NaN's runs, it's just not removing them. I'm not sure what isn't working. How do I fix my issue?
Thanks
fid = fopen('filename.txt');
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1) ...
, Rows{1,1}(20:end, :));
fid(isnan(fid(:,1)),:) = [];

채택된 답변

Stephen23
Stephen23 2015년 2월 18일
편집: Stephen23 2020년 8월 15일
The code has multiple issues that need to be fixed. Here a a couple of things to improve:
  • The fopen documentation states that fid is an integer file identifier .... The variable fid does not contain the file data, it is merely a reference to an open file. And it is also a scalar value. Therefore your attempt to index into fid as if it were a data array doesn't make any sense.
  • Over-complicated method of reading a text file: first textscan, then cellfun calling textscan again, all just to avoid some header lines? Instead you should read textscan 's documentation, and use the HeaderLines option, like this:
fid = fopen('filename.txt', 'r');
data = textscan(fid, '%f', 'delimiter','\t', 'HeaderLines',20);
fclose(fid)
I also removed the CollectOutput option, as this is superfluous if there is only one format specifier. For the same reason the delimiter doesn't really make sense either. If you have multiple values per line, then you need to specify them in the formatSpec: there are plenty of examples in the documentation.
Starting in R2018b, you can use the “rmmissing” function to remove “NaN” values from an array. For example, consider the following:
A = [1,NaN,2];
B = rmmissing(A)
The result is the vector “B = [1 2]”.
In R2018a and earlier, use the “isnan” function:
A = [1,NaN,2];
B = A(~isnan(A))
  댓글 수: 5
maaham banu
maaham banu 2019년 10월 31일
Hi, how can I remove NaN values with 2015 setup?
Walter Roberson
Walter Roberson 2019년 10월 31일
X = Columns{2};
X(isnan(X)) = [];

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Erik S.
Erik S. 2015년 2월 18일
편집: per isakson 2018년 5월 25일
You can you something like this
ind = ~isnan(Columns);
Columns=Columns(ind);

Cong Dong Ngoc Minh
Cong Dong Ngoc Minh 2020년 6월 16일
You can try this way
idx =isnan(Columns)
Columns(idx,:) = []

berfin karabulut
berfin karabulut 2020년 8월 14일
or you can simply use "omitnan" function?
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 8월 15일
omitnan is not a Mathworks function. It is an option that can be used in some functions that are not relevant to the question asked.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by