Converting string to number in table with mixed values
显示 更早的评论
Hey, trying to replace a letter with a number in a table but having a lot of trouble with competing data types. I've tried some of the other answers (thanks!) but I can't get everything back into one table. I can get the letters to transform into a numbers, but then it isn't recognized by MATLAB as a number. In the end, I need the data to be in a table as numeric values to be compatible with other scripts I have written. Thanks in advance for you help.
data=table([1;2;3],{'C';'C';'H'},[0;0;0],[4.04;5.26;5.50],[3.4;2.72;2.18],[6.55;6.49;7.23], ...
'VariableNames',{'number','atom','charge','x','y','z'});
letters=table2array(data(:,2));
[lngth,wdth]=size(data);
conv=cell(lngth,1);
%
% for k=1:lngth
% conv(k)=strrep(letters(k),'C','6')
% conv(k)=strrep(letters(k),'H','1')
% conv(k)=strrep(letters(k),'B','5')
% conv(k)=strrep(letters(k),'S','16')
%
% end
for k=1:lngth
if strcmp(letters(k),'C');
conv(k)=6;
elseif strcmp(letters(k),'H');
conv(k)=1;
elseif strcmp(letters(k),'B');
conv(k)=5;
elseif strcmp(letters(k),'S');
conv(k)=16;
end
end
%place back into table
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!