Question

I am loading some text with textscan, and although the data comes through, I would like to convert that data into a matrix of floats instead of the hh:mm:ss format.

Here is an example of the data:

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

And here is my code:

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);

Notice also that m{:,6} gets converted into ints, which is undesirable. The goal would be for b to be a single row or column of floats that can be joined from the fact that hours:minutes:seconds can be converted into a single number from 0. - 23.999

Thanks for any advice! jml

Was it helpful?

Solution

use cellfun to convert all cell elements to doubles:

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

then you can make a matrix out of the cell array using cell2mat:

m=cell2mat(m);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top