我正在使用textscan加载一些文本,虽然数据通过,我想将这些数据转换为浮点数而不是hh:mm:ss格式。
以下是数据示例:
2010/01/01,00:00:00.979131, 27.4485, 51.9362, 14.8, 6 2010/01/01,00:00:01.021977, 27.5149, 51.9375, 16.0, 6 2010/01/01,00:00:01.074032, 27.4797, 51.9446, 14.5, 10 2010/01/01,00:00:01.663689, 25.8441,-152.8141, 14.6, 6 2010/01/01,00:00:01.639541, 25.8744,-152.6122, 1.5, 5 2010/01/01,00:00:02.232099, -2.2447, 11.5023, 18.8, 6 2010/01/01,00:00:02.256351, -0.8135, 27.3139, 17.7, 5 2010/01/01,00:00:02.306734, -2.7797, 28.5109, 26.0, 5 2010/01/01,00:00:02.620765, 25.6656,-154.2029, 26.2, 9 2010/01/01,00:00:02.658495, 25.6698,-154.2157, 23.0, 6 2010/01/01,00:00:02.731266, -5.7106, 126.4517, 3.6, 5 2010/01/01,00:00:02.787495, -5.7138, 126.5210, 24.4, 8 2010/01/01,00:00:02.811636, -3.2453, 124.6919, 21.1, 8 2010/01/01,00:00:02.917785, -5.6882, 126.4566, 4.9, 7 2010/01/01,00:00:02.568360, -0.5663, 27.0334, 21.1, 6 2010/01/01,00:00:03.693717, 33.5840, 152.1755, 14.7, 6
这是我的代码:
setenv GNUTERM 'x11';
fid = fopen('data.txt', 'r');
m = textscan(fid, '%d/%d/%d %d:%d:%f %f %f %f %d', 'delimiter', ',');
%m = fscanf(fid, '%d/%d/%f');
a = [m{:,1}, m{:,2}, m{:,3}];
b = [m{:,4}, m{:,5}, m{:,6}]; %needs to contain a float
c = m{:,7};
d = m{:,8};
e = m{:,9};
f = m{:,10};
disp(b);
另请注意,m {:,6}会转换为整数,这是不可取的。 目标是使b成为单行或一列浮点数,可以从小时:分钟:秒可以从0转换为单个数字 - 23.999
感谢您的任何建议! JML
答案 0 :(得分:2)
使用cellfun
将所有单元格元素转换为双精度数:
m = cellfun(@double , m, 'UniformOutput', false);
然后你可以使用cell2mat
:
m=cell2mat(m);