Conversion to cell from double is not possible.

19 visualizaciones (últimos 30 días)
Szabó-Takács Beáta
Szabó-Takács Beáta el 7 de Sept. de 2015
Respondida: Guillaume el 7 de Sept. de 2015
I would like to translate string values are stored in cell (clim{30202,1}) to number by the following way:
clim(find(ismember(clim,{'ET'})))=1;
But I got the error:
Conversion to cell from double is not possible.
How can I solve this translation correctly?

Respuestas (2)

Stephen23
Stephen23 el 7 de Sept. de 2015
Editada: Stephen23 el 7 de Sept. de 2015
Try putting curly braces around the value you want to assign, so that you are assigning a scalar cell array instead of a scalar double:
clim(ismember(clim,{'ET'})) = {1};
I also removed the find, because logical indexing is simpler and faster. Using strcmp might be faster still:
clim(strcmp(clim,'ET')) = {1}
  3 comentarios
Szabó-Takács Beáta
Szabó-Takács Beáta el 7 de Sept. de 2015
Meantime I could fix this matter by the following way:
clim2(find(ismember(clim,{'ET'})))={1};
clim2(find(ismember(clim,{'BSk'})))={2};
where clim2 contains the numbers according to strings in clim
Stephen23
Stephen23 el 7 de Sept. de 2015
Editada: Stephen23 el 7 de Sept. de 2015
If you need a numeric array with values indicating selected clim strings, then this is much faster and simpler:
>> clim = {'ET','X','Bsk','Y','ET','Z'};
>> out(strcmp('ET',clim)) = 1;
>> out(strcmp('Bsk',clim)) = 2
out =
1 0 2 0 1
But because you have not actually told use what you are trying to achieve, and what the input variable are, then this is all guessing in the dark. Do you only have to check two strings, or an arbitrary number of them? What is your desired output?

Iniciar sesión para comentar.


Guillaume
Guillaume el 7 de Sept. de 2015
Szabó-Takács, in response to your comment to Stephan's answer, the reason you're getting an error on your second pass through clim (and why you had to use clim2) is because on your first pass you've changed some of the content of the cells from string to number. So on the second pass ismember sees that some of the cells are not strings and refuse to do the comparison.
In any case, if all you want to do is create a mapping from specific strings to sequential numbers then you certainly don't need to run multiple passes. All you need to do is use the second return value of ismember once.
clim = {'ET','X','Bsk','Y','ET','Z'}; %hypothetical data set for demonstration
[~, clim2] = ismember(clim, {'ET', 'Bsk', 'X'}) %convert each 'ET' to 1, 'Bsk' to 2, 'X' to 3, anything else to 0

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by