Removing NaN from matrix with strings and numbers

I'm having trouble removing NaN from this matrix
cabos=
[1x26 char] 'Início' 'Fim' [1x20 char] 'N. volt. level [KV]'
[2.0088e+03] 'SE S M Portuzelo' 'PTD 0526' [ 18.6057] [ 15]
[ 169.0442] 'PTC 5452' 'PTD 0450' [ 18.6057] [ 15]
[ 446.3509] 'PTD 0039' 'PTC 2850' [ 18.6057] [ 15]
[ 635.2552] 'PTD 0450' 'PTD 0039' [ 18.6057] [ 15]
[ 255.5464] 'PTD 0450' 'PTD 0090' [ 18.6057] [ 15]
[4.2281e+03] 'PTD 0526' 'PTC 5452' [ 18.6057] [ 15]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
I tried this code and it removes de NaN values, but it completely rearanges the matrix and i can't solve this
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []

5 Comments

With what do you want to replace the NaNs? Empty matrices? something else?
Or do you want to remove the rows that are made up of NaNs only? What happens if only some columns of the row have NaNs?
the matrix is compact. there are never NaN in the middle of the columns or rows. i just want to remove them to have the matriz in it's real size.
your help didn't do the trick, i still have the same error.
Undefined function 'isnan' for input arguments of type 'cell'.
Error in @(c)isscalar(c)&&isnan(c)
Your matrix is actually a cell array (since it contains a combination of numbers and strings). These are two very different things in matlab, so use the proper terminology.
From the snippet you've posted I've assumed that each cell of the cell array either contain a string or a scalar number. However, if you get the error you mention with my code (you'll always get it with Stalin's), then it means that there are cell arrays in those cells. Thus, can you attach your cabos as a .mat file?
Can you also confirm that what you want to do is remove any row that contain NaN?
it's a really big project i'm working on and it's all in portuguese soi'm going to try to explain myself better. i'm extracting from an excel file a table, call it A.
A=
[1x26 char] 'Início' 'Fim' [1x20 char] [ NaN]
[2.0088e+03] 'SE S M Portuzelo' 'PTD 0526' [ 18.6057] [ NaN]
[ 169.0442] 'PTC 5452' 'PTD 0450' [ 18.6057] [ NaN]
[ 446.3509] 'PTD 0039' 'PTC 2850' [ 18.6057] [ NaN]
[ 635.2552] 'PTD 0450' 'PTD 0039' [ 18.6057] [ NaN]
[ 255.5464] 'PTD 0450' 'PTD 0090' [ 18.6057] [ NaN]
[4.2281e+03] 'PTD 0526' 'PTC 5452' [ 18.6057] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
note that the table is much larger, i just took a sample.
all my attempts got me that same error
Undefined function 'isnan' for input arguments of type 'cell'
unless this one that removed all of the NaN but completely disformed my matrix
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []
If your code
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []
does not give you the error Undefined function 'isnan' for input arguments of type 'cell', then neither would mine.
I suspect you've tried Stalin's code, whose first line modified your cabos cell array. And yes, after that, on that my code (but yours as well) you would get the error.
Reload your cabos from file or do:
cabos = cabos{1};

Sign in to comment.

 Accepted Answer

Possibly, this is what you want. But see comments to your question.
cabos(cellfun(@(c) isscalar(c) && isnan(c), cabos)) = {[]}
This will replace NaNs with empty matrices. The benefit of which is not obvious to me.

9 Comments

Diogos wrote: your help didn't do the trick, i still have the same error. Undefined function 'isnan' for input arguments of type 'cell'.
This means that there are cell arrays inside your cell array (which does not appear to be the case in the example you've posted).
So, to remove rows containing scalar NaNs:
cabos(all(cellfun(@(c) isnumeric(c) && isscalar(c) && isnan(c), cabos), 2), :) = []
edit: If your original cabos does not contain cell arrays and instead you've tried my code on the cabos modified by Stalin's, then this:
cabos(all(cellfun(@(c) isscalar(c) && isnan(c), cabos), 2), :) = []
should work with the original cabos.
ok, so it removes all the NaN rows but the NaN columns are still there. thanks in advance!
To remove the NaN columns:
cabos(all(cellfun(@(c) isscalar(c) && isnan(c), cabos), 1), :) = []
another error Matrix index is out of range for deletion.
sorry to be so anoying, i haven't slept for a while
Yes, sorry, forgot the swap the row/columns, should have been:
cabos(:, all(cellfun(@(c) isscalar(c) && isnan(c), cabos), 1)) = []
actually i don't think it's doing anything.. i tried to remove just the columns without removing the rows and it didn't do anything
it works!! thanks Guillaume!
Hi ... I am currently working on similar problem, however, I want to replace NaN in double/logical array with character 'A' to appear ... how can I do it ... Many thanks for any help
@Vasishta Bhargava: Please do not hijack a thread by appending a new question as comment to an answer. Open a new thread instead.
Then ths answer will be: This cannot work. You cannot insert characters in a numerical or logical array. All arrays, except for cell arrays, must have the same data type for all elements.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 29 Jan 2015

Commented:

Jan
on 28 Jun 2018

Community Treasure Hunt

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

Start Hunting!