Data conversion cell to double and char
5 次查看(过去 30 天)
显示 更早的评论
I have the following data
column values as follows (in Cell format): in the file name of: Data_All{j}{k,1}
2.3 mm
4.6 %
Normal
False Alarm
But I want store as below
2.3--> Double
4.6--->Double
Normal
False Alarm
When I use
for k=1:size(Data_All{j},1)
Data_Allfinal{j}{k,1}=Data_All{j}{k,1};
final11=strsplit(char(Data_Allfinal{j}{k,1}), ' ');
f31{j}{k,1}=str2double(char(final11(1)));
end
it is returning as below
2.3--->Double
4.6-->Double
NaN
NaN
Can some one help me how to store numerical values in double format, and other as char form (I want to avoid NaN (keep their original as such)
Thanks in advance.
0 个评论
回答(1 个)
Guillaume
2014-11-22
You're using str2double on your strings as well. Only use it for numbers and leave the strings as is.
2 个评论
Guillaume
2014-11-22
One way to do it would be copy the whole cell array and then only replace the columns you want to convert with the for loop:
f31{j} = Data_all{j}
colstoconvert = [1, 2]; %following your example
for col = colstoconvert
splitrow = strsplit(Data_all{j}{col}, ' '); %no need for char
f31{j}{col} = str2double(splitrow{1}); %no need for char
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!