Plotting timeseries data in MATLAB -
i have matrix contains 2 columns of data. first column has timestamps given in unix time , second column has corresponding data set.
i'm trying plot data
human-readable times on bottom axis.
i've plotted raw data, so:
plot(data(:,1), data(:,2));
i know there timeseries()
function in matlab, can't seem working correctly. should able plot data, following matlab documentation.
i've tried declaring first column time series:
ts = timeseries(data(:,1));
then tried plotting data, so:
plot(ts, data(:,1));
although approach seems make rational sense, following error:
error using plot data must single matrix y or list of pairs x,y
i tried use addsample()
function append data time series , plot it.
k = addsample(ts, data(:,2)); plot(k);
but doing produced following error:
a new sample must specified either structure or property-value pairs.
so how plot time data correctly? thank you!
i work quite posix
time (i.e. unixtime
) other programs, in matlab easiest format deal time , date matlab time serial number format.
to convert unix
matlab
use small conversion function extensively:
function matlabtime = unix2matlabtime(unixtime) %// function matlabtime = unix2matlabtime(unixtime) %// %// input : ** unixtime ** : time vector in unix time serial number %// representation (seconds since 01-jan-1970) %// %// output : ** matlabtime ** : time vector in matlab time serial number %// representation (days since 01-jan-0000) pivot = datenum([1970 01 01 00 00 00]) ; matlabtime = ( unixtime / 24 / 3600 ) + pivot ;
with function saved somewhere on path, can plot data so:
%// generate sample data sampleunixtime = linspace( 1427205640 , 1427205900 ).' ; %'// ignore comment data = [sampleunixtime , sin(sampleunixtime./10) ] ; %// time vector in matlab time serial format, plot time = unix2matlabtime( data(:,1) ) ; plot( time, data(:,2) ) %// adjust x-ticks in human readable format set( gca , 'xticklabel' , datestr( get(gca,'xtick'),'hh:mm:ss' ) )
to obtain:
look @ datenum
, datestr
documentation see how handle these. there many predefined output format date/time or can build own refine precision need (add millisecond, remove seconds, add date etc ...).
just aware xticklabel
overriden, not updated automatically. if zoom
or pan
on figure you'll have re-run last line of code refresh xticks
values.
(personally, put last line of code in matlab toolbar shortcut have quick access @ time).
Comments
Post a Comment