Reading all values as a string from excel file

43 visualizaciones (últimos 30 días)
Sarah R
Sarah R el 24 de En. de 2022
Comentada: Voss el 24 de En. de 2022
I have an excel file that includes, strings, numbers and time in the format "01:59.00"
When I use xlsread in Matlab to read in the sheet I am currently using [~, ~, alldata] = xlsread... however, my time variables are getting converted to decimals.
Is there any way I am able to keep the time variable or even read it as a string?
Thank you in advance
  1 comentario
Stephen23
Stephen23 el 24 de En. de 2022
Editada: Stephen23 el 24 de En. de 2022
"my time variables are getting converted to decimals."
Nothing is "getting converted", in fact Excel stores dates as serial date numbers:
Ultimately all data stored on an Excel worksheet are either text or numeric: dates are numeric, so XLSREAD is giving you the actual data saved in the workbook. Formatting in Excel that interprets that serial date number and displays it in particular ways is purely an artifact of your OS's locale settings or the cell formatting.

Iniciar sesión para comentar.

Respuestas (2)

Voss
Voss el 24 de En. de 2022
Maybe you can just convert the decimal times to datetimes using the datetime() function, after reading them in.
now()
ans = 7.3855e+05
datetime(now(),'ConvertFrom','datenum')
ans = datetime
24-Jan-2022 01:24:14
  2 comentarios
Stephen23
Stephen23 el 24 de En. de 2022
Editada: Stephen23 el 24 de En. de 2022
How does this account for the different epochs used by MATLAB's and Excel's serial date numbers ?
Voss
Voss el 24 de En. de 2022
I'm going to guess it does not.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 24 de En. de 2022
In Excel dates are stored as serial date numbers, so what you are getting is the raw data.
The best solution is to use READMATRIX or READCELL which will (either automagically or with some hints from you) import Excel's serial date numbers as DATETIME objects.
The next option, if you insist on using outdated XLSREAD, is to do that conversion yourself. There are many threads on this forum showing how, the simplest approach is to use DATETIME which has this conversion built in:
N = 39448.25 % Excel serial date number
N = 3.9448e+04
D = datetime(N,'convertFrom','excel')
D = datetime
01-Jan-2008 06:00:00

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by