How to read time as string data from excel file

15 visualizaciones (últimos 30 días)
Yasuyuki Hamanaka
Yasuyuki Hamanaka el 24 de Mayo de 2021
Comentada: Yasuyuki Hamanaka el 24 de Mayo de 2021
I used readtable to read time from excel spread sheets. The time was converted to double like e.x. But I want to read time as original form. How does read time as string?
e.x 10:11:09 AM => 0.42858

Respuesta aceptada

Yutaka Yamada
Yutaka Yamada el 24 de Mayo de 2021
How about the below code if you want to use readtable?
opts = detectImportOptions('readTime.xlsx');
opts.VariableTypes = 'datetime';
T = readtable('readTime.xlsx', opts);
for i = 1:3
a = T{i, 1};
a.Format = 'HH:mm:ss a';
end
I've attached the Excel file that I've used for this test also.
  3 comentarios
Yasuyuki Hamanaka
Yasuyuki Hamanaka el 24 de Mayo de 2021
Thank you both for answering. Actually the spreadsheet is the picture below. I want to read Var1 as date, Var2 as time and Var 3 and 4 asdouble. Do you know how to specify the format of each columns, if there's multiple variables?
Yasuyuki Hamanaka
Yasuyuki Hamanaka el 24 de Mayo de 2021
I found the function setvaropts.
opts = detectImportOptions('input.xlsx');
opts = setvartype(opts,{'Var2'},'datetime');
opts = setvaropts(opts,{'Var2'},'DatetimeFormat','HH:mm:ss a');
schedule = readtable('input.xlsx',opts);
Thank you everyone for helping me. Your information were very helpful and I learned a lot.

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 24 de Mayo de 2021
Hi,
You should try using xlsread() in this case. In fact, xlsread is slower than readtable().
Then you can employ datenum() and datestr() if necessary.
Good luck.
  1 comentario
Yasuyuki Hamanaka
Yasuyuki Hamanaka el 24 de Mayo de 2021
Thank you for answering my question. I want to use readtable because the amount of data is so large that reading data takes long. But I didn't know the fucntion of datenum() and datestr(), so it is very helpful for me.

Iniciar sesión para comentar.

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by