我正在尝试绘制每小时数据。问题是日期在单元格数组的一列中,而小时在下一列中。考虑到日期和时间,如何使用 x 轴绘制数据?我认为最好的方法是将这两列合并并将其用作我的 x。
% Plot data for the month of June, 2007
% Uses the FileExchange file addaxes.m to plot 3 different y axes to
% account for the different units
clear
clc
load WI_200706_Pollution.mat;
%% Order of Columns
% 5. State Code
% 6. County Code
% 7. Site Num
% 13. Date GMT
% 14. 24 Hour GMT
% 17. Sample Measurement
%% Sort data by State Code, County Code, Site Code, Date GMT, Hour GMT
% PM25
PM25_State = sortrows(PM25,5);
PM25_County = sortrows(PM25_State,6);
PM25_Site = sortrows(PM25_County,7);
PM25_Date = sortrows(PM25_Site,13);
PM25_Hour = sortrows(PM25_Date, 14);
% SO2
SO2_State = sortrows(SO2,5);
SO2_County = sortrows(SO2_State,6);
SO2_Site = sortrows(SO2_County,7);
SO2_Date = sortrows(SO2_Site,13);
SO2_Hour = sortrows(SO2_Date, 14);
% Pull out data
PM25_data = PM25_Hour(:,17); % ug/L3
NO2_data = NO2_Hour(:,17); % ppb
CO_data = CO_Hour(:,17); % ppm
O3_data = O3_Hour(:,17); % ppm
SO2_data = SO2_Hour(:,17); % ppb
clearvars -except -regexp _data _Hour % Clear all variables except the sorted _Hour files and the _data files
%%%%%%%
%% Plot PM2.5 daily data
% Axis
DateString = PM25_Hour(2:end,11); % Pull out dates
formatIn = 'yyyy-mm-dd';
x1 = datenum(DateString,formatIn); % Convert to datnum
y1 = PM25_data(2:end); % Plot data
%% Plot SO2 hourly data
% Axis
date = datenum(SO2_Hour(2:end, 11));
hour = datenum(SO2_Hour(2:end, 12));
DateString = datenum(strcat(date, hour), 'yyyy-mm-ddHH:MM'); % Pull out dates
x5 = datenum(DateString); % Convert to datnum
y5 = SO2_data(2:end); % Plot data
% Convert cells to double
y1 = str2double(y1);
y5 = str2double(y5);
% Plot
h = figure; % To make the figure not show up, use 'h = figure('visible', 'off')';
plot(x5, y5, 'k', 'LineWidth', 1, 'Marker', 'o'); % Black line of width 2. Circular marker of color blue
addaxis(x1, y1);
addaxislabel(1,'ppb');
addaxislabel(2,'ug/CL');
grid on; % Turn on grid for plot
legend('PM2.5 (ug/CL', 'SO2 (ppb)');
代码已被缩短,只包含我正在查看的五个变量中的两个。PM2.5 是每日数据,所以没有问题。对于 SO2,我试图在“绘制 SO2 每小时数据”部分下结合日期和时间。现在,我收到此错误:
Error using datenum
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to
date number.
如果我不结合日期和时间,我每天都会得到一堆垂直向上的点。我怎样才能仍然每天保持 PM2.5 的 x 轴,但让 SO2 在一天中的几个小时内分散?