Removing NaN from matrix with strings and numbers

10 次查看(过去 30 天)
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 个评论
Diogo Queirós
Diogo Queirós 2015-1-29
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)) = []
Guillaume
Guillaume 2015-1-29
编辑:Guillaume 2015-1-29
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};

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2015-1-29
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 个评论
VBBV
VBBV 2018-6-28
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
Jan
Jan 2018-6-28
@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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by