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 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2023-6-21
编辑:the cyclist 2023-6-21
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).

更多回答(2 个)

Deepak
Deepak 2023-6-21
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 个评论
Eder Hernandez Martinez
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. :(

请先登录,再进行评论。


Steven Lord
Steven Lord 2023-6-21
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

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by