Trouble loading in mixed data from txt file

I'm trying to load in data from a text file. The first two rows are headers, following the headers the first two columns are date and time. The rest of the columns are floats.
data should have 11 columns, however, whos returns that size is only 1x3
fid = fopen('allunderway.txt', 'rt');
data = textscan(fid, '%{M/dd/yyyy}D %{HH:mm:ss}D %4.2f %2.4f %2.5f %2.4f %2.4f %2.2f %4.2f %3.1f %1.4f', 'HeaderLines', 2, 'CollectOutput', true);
fclose(fid);
whos data
date = data{1};
time = data{2};
wnd_td = data{10};
wnd_ts = data{11};

 Respuesta aceptada

Jeremy Hughes
Jeremy Hughes el 30 de Mayo de 2018

1 voto

You're adding 'CollectOutput' which is concatenating all the numeric columns into one. If you remove that, you should get the number of columns you expect.

Más respuestas (1)

KSSV
KSSV el 30 de Mayo de 2018
Try this:
fid = fopen('allunderway.txt', 'rt');
data = textscan(fid, '%{M/dd/yyyy}D %{HH:mm:ss}D %4.2f %2.4f %2.5f %2.4f %2.4f %2.2f %4.2f %3.1f %1.4f', 'HeaderLines', 2, 'Delimiter','\n', 'CollectOutput', true);
fclose(fid);
whos data
date = data{1};
time = data{2};

2 comentarios

Marissa Menzel
Marissa Menzel el 30 de Mayo de 2018
I tried your version and got an error message for the dates.
Error using textscan Unable to read the DATETIME data with the format 'M/dd/yyyy'. If the data is not a time, use %q to get text data.
Error in windconversions (line 21) data = textscan(fid, '%{M/dd/yyyy}D %{HH:mm:ss}D %4.2f %2.4f %2.5f %2.4f %2.4f %2.2f %4.2f %3.1f %1.4f', 'HeaderLines', 2, 'Delimiter','\n', 'CollectOutput', true);
Marissa Menzel
Marissa Menzel el 30 de Mayo de 2018
I tried changing both to %q as recommended and the error went away but the matrix dimensions for data were still only 1x2

Iniciar sesión para comentar.

Productos

Versión

R2016b

Preguntada:

el 29 de Mayo de 2018

Respondida:

el 30 de Mayo de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by