Conversion to cell from double is not possible.
7 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Stephen23
on 7 Sep 2015
Edited: Stephen23
on 7 Sep 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 Comments
Stephen23
on 7 Sep 2015
Edited: Stephen23
on 7 Sep 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?
Guillaume
on 7 Sep 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
0 Comments
See Also
Categories
Find more on Cell Arrays 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!