How can I remove NaN values from a matrix?

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)),:) = [];

 Respuesta aceptada

Stephen23
Stephen23 el 18 de Feb. de 2015
Editada: Stephen23 el 15 de Ag. de 2020
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 comentarios

So, I tried to remove the NaN's using the method you suggested. However, I still have an error. All of them are listed below.
X = Columns(2);
X(isnan(X)) = []
My error is:
Undefined function 'isnan' for input arguments of type 'cell'.
Error in SecondScript (line 12) X(isnan(X)) = []
I'm not sure what is wrong...
Columns is a cell array and X is a cell array, too. Either get X as content of the cell:
X=Columns{2};
and proceed as you did or check it inside the cell:
X={X{1}(~isnan(X{1}))};
Stephen23
Stephen23 el 19 de Feb. de 2015
Editada: Stephen23 el 19 de Feb. de 2015
Continuing Michael Haderlein's comment: If you are going to use MATLAB you need to learn the different ways of indexing cell arrays :
  • {} Curly Braces refer to the contents of a cell array.
  • () Parentheses refer to the cells themselves.
This means if you have a cell array and you want the numeric data from inside that cell array, use the curly braces (note that textscan always returns a cell array!). And yes, both of these different indices are useful!
PS: It is considered polite on this forum to Accept an answer that best resolves your question.
maaham banu
maaham banu el 31 de Oct. de 2019
Hi, how can I remove NaN values with 2015 setup?
X = Columns{2};
X(isnan(X)) = [];

Iniciar sesión para comentar.

Más respuestas (3)

Erik S.
Erik S. el 18 de Feb. de 2015
Editada: per isakson el 25 de Mayo de 2018
You can you something like this
ind = ~isnan(Columns);
Columns=Columns(ind);
Cong Dong Ngoc Minh
Cong Dong Ngoc Minh el 16 de Jun. de 2020

0 votos

You can try this way
idx =isnan(Columns)
Columns(idx,:) = []
berfin karabulut
berfin karabulut el 14 de Ag. de 2020

0 votos

or you can simply use "omitnan" function?

1 comentario

Walter Roberson
Walter Roberson el 15 de Ag. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 18 de Feb. de 2015

Editada:

el 15 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by