matlab 读取Excel时间列,每隔一小时取一次数据。写出相关代码
时间: 2024-05-08 18:18:56 浏览: 97
Sure, here is the code to read time column data from an Excel file in Matlab and retrieve data every hour:
```
% Specify Excel file and sheet name
filename = 'example.xlsx';
sheet = 'Sheet1';
% Read time column data from Excel file
[~, timeData] = xlsread(filename, sheet, 'A2:A100');
% Convert time data to Matlab datetime format
timeData = datetime(timeData, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
% Specify duration of time step
timeStep = hours(1);
% Initialize variables to store time and data
desiredTimes = datetime([], [], [], [], [], [], 'TimeZone', 'Asia/Shanghai');
data = [];
% Loop through time column data and retrieve data every hour
for i = 1:length(timeData)
if mod(i, timeStep) == 1 % Only retrieve data every hour
desiredTimes(end+1) = timeData(i);
data(end+1) = xlsread(filename, sheet, ['B' num2str(i+1)]);
end
end
% Display retrieved data
disp(data);
```
Note: This code assumes that the time column data are in the format 'yyyy-MM-dd HH:mm:ss' and that the data to be retrieved are in the column next to the time column. You may need to adapt the code to your specific situation.
阅读全文