www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/PlotDatesAndDurationsExample.m

    %% Plot Dates and Durations
% This example shows how to create line and scatter plots of datetime and
% duration values using the |plot| function. Then, it shows how to convert
% datetime and duration values to numeric values to create other
% types of plots.
%% Line Plot with Dates
% Create a line plot with datetime values on the x-axis. 
%%
% Define |t| as a sequence of dates.
t = datetime(2014,6,28) + caldays(1:10);
%%
% Define |y| as random data. Then, plot the vectors using the |plot|
% function.
y = rand(1,10);
plot(t,y);
%%
% By default, |plot| chooses tick mark locations based on the range of
% data. When you zoom in and out of a plot, the tick labels automatically
% adjust to the new axis limits.
%%
% Replot the data and specify a format for the datetime tick labels, using
% the |DatetimeTickFormat| name-value pair argument.
plot(t,y,'DatetimeTickFormat','dd-MMM-yyyy')
%%
% When you specify a value for the
% |DatetimeTickFormat| argument,  |plot| always formats the tick labels
% according to the specified value.
%% Line Plot with Durations
% Create a line plot with duration values on the x-axis. 
%%
% Define |t| as seven linearly spaced duration values between 0 and 3
% minutes. Define |y| as a vector of random data.
t = 0:seconds(30):minutes(3);
y = rand(1,7);
%%
% Plot the data and specify the format of the duration tick marks in terms
% of a single unit, seconds.
h = plot(t,y,'DurationTickFormat','s');
%%
% View the x-axis limits
xlim
%%
% The x-axis limits are stored as numeric values in units of seconds.
%%
% Change the format of the duration tick marks by replotting the same data.
% Specify the format of the duration tick marks in the form of a
% digital timer that includes more than one unit.
figure
h = plot(t,y,'DurationTickFormat','mm:ss');
%%
% View the x-axis limits
xlim
%%
% The x-axis limits are stored in units of 24-hour days when the duration
% tick format is not a single unit.
%% Change Axis Limits
% Plot dates and random data.
t = datetime(2014,6,28) + calweeks(1:10);
y = rand(1,10);
plot(t,y);
%%
% View the x-axis limits
format longG
xlim
%%
% The axis limits for datetime values are stored as serial date numbers.
%%
% Change the x-axis limits by specifying the new bounds in terms of serial date numbers. Use the |datenum| function to create serial date numbers.
xmin = datenum(2014,7,14)
%%
xmax = datenum(2014,8,23)
%%
% Specify the new x-axis limits using the |xlim| function.
xlim(datetime(2014,[7 8],[14 23]))
%% Properties Stored as Numeric Values
% In addition to axis limits, the locations of tick labels and the x-, y-,
% and z-values for dates and durations in line plots are also stored as
% numeric values. The following properties represent these aspects of line
% plots.
% 
% * |XData|, |YData|, |ZData|
% * |XLim|, |YLim|, |ZLim|
% * |XTick|, |YTick|, |ZYTick|
% 
%% Scatter Plot
% Create a scatter plot with datetime or duration inputs using the |plot|
% function. The |scatter| function does not accept datetime and duration inputs.
%%
% Use the |plot| function to create a scatter plot,
% specifying the marker symbol, |'o'|.
t = datetime('today') + caldays(1:100);
y = linspace(10,40,100) + 10*rand(1,100);
plot(t,y,'o')
%% Other Plot Types
% Other MATLAB plotting functions do not accept datetime and duration
% inputs. Convert datetime and duration values to numeric values before plotting
% them using a function other than |plot|.
%%
% Convert the datetime values in |t| to numeric values by subtracting a
% datetime origin. 
dt = t - datetime(2010,9,1);
%%
% |dt| is a |duration| array. Convert |dt| to a |double| array of values in
% units of years, days, hours, minutes, or seconds using the |years|, |days|,
% |hours|, |minutes|, or |seconds| function, respectively.
x = days(dt);
whos x
%%
% Plot |x| using the |bar| function, which accepts |double| inputs.
bar(x,y)