Removing NaN from matrix with strings and numbers
Show older comments
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
Guillaume
on 29 Jan 2015
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?
Diogo Queirós
on 29 Jan 2015
Guillaume
on 29 Jan 2015
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?
Diogo Queirós
on 29 Jan 2015
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};
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!