2

我正在使用 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

4

1 回答 1

2

用于cellfun将所有单元格元素转换为双精度:

m = cellfun(@double , m, 'UniformOutput', false);

然后您可以使用以下方法从元胞数组中创建一个矩阵cell2mat

m=cell2mat(m);
于 2012-12-31T23:05:03.903 回答