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
16 views (last 30 days)
Show older comments
Eder Hernandez Martinez
on 21 Jun 2023
Commented: Eder Hernandez Martinez
on 22 Jun 2023
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
1 Comment
the cyclist
on 21 Jun 2023
Better to upload your Excel data than to describe it. You can use the paper clip icon in the INSERT section of the toolbar.
Accepted Answer
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)
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
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.
Steven Lord
on 21 Jun 2023
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)
0 Comments
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!