Clear Filters
Clear Filters

Hello everyone, I have date and time data in an excel separately. That is, year in one column, month in another, day in another column, and time in another column. how can i r

17 views (last 30 days)
I have 1,374 data in my excel, and I have the date and time in each column (just as I put it below) and I would like to know how do I get matlab to read my dates?...I already tried making a vector by extracting the 4 columns, but I think It doesn't work that way... I also tried to concatenate directly from Excel, but it didn't work either. Someone who can guide me how to do it?... Thank you very much!
2023 4 21 20
2023 4 21 21
2023 4 21 22
2023 4 21 23
2023 4 22 0
2023 4 22 1
2023 4 22 2

Accepted Answer

the cyclist
the cyclist on 21 Jun 2023
Edited: the cyclist on 21 Jun 2023
Here is one way:
dateTbl = readtable("dateFile.xlsx");
dateData = datetime(dateTbl.Var1,dateTbl.Var2,dateTbl.Var3,dateTbl.Var4,0,0)
dateData = 7×1 datetime array
21-Apr-2023 20:00:00 21-Apr-2023 21:00:00 21-Apr-2023 22:00:00 21-Apr-2023 23:00:00 22-Apr-2023 00:00:00 22-Apr-2023 01:00:00 22-Apr-2023 02:00:00
The exact syntax might vary from the above, depending on the format of your Excel file (e.g. if it has a header row).

More Answers (2)

Deepak
Deepak on 21 Jun 2023
To import the dates and times from Excel into MATLAB, you can use the "xlsread" function. The function reads the data from the Excel file and returns the data in MATLAB variables that you can work with in your program. Here are the steps you can follow:
1. Save your Excel file in a directory accessible to MATLAB.
2. In MATLAB, open the Command Window and type the following command:
[data, text, all_data] = xlsread('filename.xlsx');
Replace "filename.xlsx" with the name of your Excel file. The "data" output argument contains the numeric data from the Excel file, while the "text" output argument contains any text data in the file. The "all_data" output argument contains everything - text and numeric data - in the Excel file.
3. Extract the date and time data from the "data" variable. Assuming that the date is in the first three columns and the time is in the fourth column, you can extract the date and time data using the following code:
dates = datetime(data(:, 1:3));
times = timeofday(datetime(data(:, 4), 'ConvertFrom', 'datenum'));
The first line creates a "datetime" array from the date columns in the "data" variable. The second line creates a "datetime" array from the time column by converting the MATLAB serial date number to a "datetime" object, then extracting the time of day from that object.
4. Combine the date and time data into a single "datetime" array:
datetime_array = dates + times;
This step adds the "dates" and "times" arrays to create a single array with both date and time information.
Now you can work with the "datetime_array" variable to analyze or visualize your data in MATLAB.
  2 Comments
Eder Hernandez Martinez
Eder Hernandez Martinez on 22 Jun 2023
Hi @Deepak! I tried to do it as you suggested, but it gives me an error in the line dates = datetime(data(:, 1:3));...it tells me that it does not have a format or something like that and I tried to fix that line, but i didn't make it. :(

Sign in to comment.


Steven Lord
Steven Lord on 21 Jun 2023
Read the data into MATLAB using your favorite approach then call datetime.
data = [...
2023 4 21 20
2023 4 21 21
2023 4 21 22
2023 4 21 23
2023 4 22 0
2023 4 22 1
2023 4 22 2];
Since your data only has 4 columns (year, month, day, hour) you need to add two columns of 0's (for minutes and seconds.)
dataWithMinutesAndSeconds = [data, zeros(height(data), 2)];
dt = datetime(dataWithMinutesAndSeconds)
dt = 7×1 datetime array
21-Apr-2023 20:00:00 21-Apr-2023 21:00:00 21-Apr-2023 22:00:00 21-Apr-2023 23:00:00 22-Apr-2023 00:00:00 22-Apr-2023 01:00:00 22-Apr-2023 02:00:00

Tags

Community Treasure Hunt

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

Start Hunting!