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

 채택된 답변

Yutaka Yamada
Yutaka Yamada 2021년 5월 24일

1 개 추천

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

You should use readtable, xlsread is not recommended and is there only for legacy support. Two comments on the code above 1. For a datetime array you can directly change the Format of the entire array at once, you do not need to do it element by element. 2. You can directly specify the display format in opts as follows:
>> opts = detectImportOptions('readTime.xlsx');
>> opts.VariableTypes = 'datetime';
>> opts.VariableOptions.DatetimeFormat = 'HH:mm:ss a';
>> T = readtable('readTime.xlsx', opts)
T =
3×1 table
Var1
___________
10:11:09 AM
10:11:09 AM
10:11:09 AM
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?
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 24일

1 개 추천

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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by