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
23 ビュー (過去 30 日間)
古いコメントを表示
Eder Hernandez Martinez
2023 年 6 月 21 日
コメント済み: Eder Hernandez Martinez
2023 年 6 月 22 日
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1416454/image.png)
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 件のコメント
the cyclist
2023 年 6 月 21 日
Better to upload your Excel data than to describe it. You can use the paper clip icon in the INSERT section of the toolbar.
採用された回答
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)
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
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.
Steven Lord
2023 年 6 月 21 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!