How do I convert a cell array with string, double, and NaN values to a double array

I have a cell with these values = {1, 2, 'None', 1, 2, 3, 5, NaN, 'None'}
I want to convert this into a double with these values = {1, 2, NaN, 1, 2, 3, 5, NaN, NaN}
Is this possible?

 Respuesta aceptada

jonas
jonas el 5 de Sept. de 2018
Editada: jonas el 5 de Sept. de 2018
My original solution was obviously flawed, so credits to Guillaume for cleaning up redundant functions
A(strcmp(A, 'None')) = {NaN};
cell2mat(A)
... and credits to Stephen Cobeldick for providing the general solution (for any string):
A(~cellfun(@isnumeric,A)) = {NaN}

3 comentarios

Note that the find is completely redundant
A(strcmp(A, 'None')) = {NaN};
is simpler and faster.
No need to use find, logical indexing works perfectly.
An alternative that replaces all non-numeric values:
A(~cellfun(@isnumeric,A)) = {NaN}
Thanks guys! I put your solutions in the answer box for future reference, as the answer had already been accepted.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 5 de Sept. de 2018

Editada:

el 5 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by