Replace String with a NaN in table

21 visualizaciones (últimos 30 días)
Spacey
Spacey el 24 de Mzo. de 2015
Respondida: Peter Perkins el 24 de Mzo. de 2015
Hey guys!
I am challenged with a table, consisting various numbers and a few "'Bad'" cells. I guess they are strings. My goal now is to replace these "Bads" with NaNs.
How do I achieve that the easiest way?
Best regards, Spacey

Respuesta aceptada

Peter Perkins
Peter Perkins el 24 de Mzo. de 2015
It sounds like you imported a file where some of the columns were mostly numbers, but some strings mixed in. Best to avoid getting into the corner that you're in. You might have been able to explicitly say that the column is numeric. You might have been able to use the TreatAsEmpty parameter to readtable (if that's what you used).
Otherwise, if you have variables in the table that are cell arrays but that you want to be numeric, you might need to do one of a couple different things. In general, you need to put numeric things in all the cells and then concatenate the contents to get a numeric vector, but first you'll need to describe exactly what you have.

Más respuestas (3)

Konstantinos Sofos
Konstantinos Sofos el 24 de Mzo. de 2015
Editada: Konstantinos Sofos el 24 de Mzo. de 2015
Hi,
" I guess they are strings" ...you cannot open your table or see in the workspace what type are they?
Do you mean something like the following:
>> A = {'xx',5,6,'''',78,'?..'}
A =
'xx' [5] [6] ''' [78] '?..'
>> idx = cellfun(@(x) ischar(x),A)
idx =
1 0 0 1 0 1
>> A(idx)={NaN}
A =
[NaN] [5] [6] [NaN] [78] [NaN]
>> B = cell2mat(A)
B =
NaN 5 6 NaN 78 NaN
>> whos
Name Size Bytes Class Attributes
A 1x6 408 cell
B 1x6 48 double

Jos (10584)
Jos (10584) el 24 de Mzo. de 2015

Spacey
Spacey el 24 de Mzo. de 2015
Unfortunately I couldn't get it working yet. The huge problem is the table. It says that the "'Bad'" values are cells, but many functions don't even work with cells respectively different formats within the table. Do guys have any other ideas or questions that would specify this problem?

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by