MATLAB结合两个单元格列的日期和小时

时间:2013-11-08 20:26:07

标签: matlab date plot

我正在尝试绘制每小时数据。问题是日期位于单元格数组的一列中,而小时位于下一列中。考虑到日期和时间,我如何用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在一天中的几个小时内散开?

1 个答案:

答案 0 :(得分:1)

您的日期格式无效,因为2000-12-111:00可能是2000-12-11 1:002000-12-1 11:00。使用分隔符。

如果这不是错误:输入datenum是什么意思?尝试dbstop if error然后它会停在右边。


更新此修复错误,没有理由将日期和小时转换为双倍:

date = SO2_Hour(2:end, 11); 
hour = SO2_Hour(2:end, 12);

DateString = datenum(strcat(date,'-', hour), 'yyyy-mm-dd-HH:MM'); % Pull out dates