How to convert cell array containing text to numbers?
5 次查看(过去 30 天)
显示 更早的评论
I have a cell array containing a mixture of numbers and letters. I want to remove all the letters and keep only the numbers in it. The cell array is:
>> A(3:12,:)
ans =
10×4 cell array
{'60E' } {'8.571307N'} {'Jun-Sep 1979'} {[9.5850e-06]}
{'61.875E'} {'8.571307N'} {'Jun-Sep 1979'} {[3.1450e-05]}
{'63.75E' } {'8.571307N'} {'Jun-Sep 1979'} {[7.1122e-05]}
{'65.625E'} {'8.571307N'} {'Jun-Sep 1979'} {[7.2475e-05]}
{'67.5E' } {'8.571307N'} {'Jun-Sep 1979'} {[9.0740e-05]}
{'69.375E'} {'8.571307N'} {'Jun-Sep 1979'} {[9.7142e-05]}
{'71.25E' } {'8.571307N'} {'Jun-Sep 1979'} {[1.1910e-04]}
{'73.125E'} {'8.571307N'} {'Jun-Sep 1979'} {[1.6324e-04]}
{'75E' } {'8.571307N'} {'Jun-Sep 1979'} {[1.6622e-04]}
{'76.875E'} {'8.571307N'} {'Jun-Sep 1979'} {[1.4362e-04]}
But I want to convert it into a numeric array containing numbers only. For example, the first 2 rows of the modified array should look like:
A=[60 8.571307 1979 9.5850e-06; 61.875 8.571307 1979 3.1450e-05];
How to do that?
0 个评论
采纳的回答
Matt J
2021-4-3
编辑:Matt J
2021-4-3
I don't think there is any solution that will work without any assumptions about the alphabetic content of the cells. Here, I assume that undesired text is either entirely to the left or right of the desired number in each cell:
A={'1.3E','Jun-Sep 2.6'}
for i=1:numel(A)
tmp=A{i};
if ~ischar(tmp), continue; end
m=isletter(tmp);
j=find(m,1); k=find(m,1,'last');
tmp(j:k)='';
A{i}=str2double(tmp);
end
A=cell2mat(A)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!