简体   繁体   中英

Plotting timeseries data in MATLAB

I have a matrix that contains two columns of data. The first column has timestamps given in UNIX time and the second column has a corresponding data set.

I'm trying to plot this DATA with human-readable times on the bottom axis.

I've plotted the raw data, like so:

plot(DATA(:,1), DATA(:,2));

I know there is a timeseries() function in MATLAB, but I can't seem to get it working correctly. I should be able to plot the data, following the MATLAB documentation .

I've tried declaring the first column as a time series:

TS = timeseries(DATA(:,1));

Then I tried plotting the data, like so:

plot(TS, DATA(:,1));

Although this approach seems to make rational sense, I get the following error:

Error using plot Data must be a single matrix Y or a list of pairs X,Y

I also tried to use the addsample() function to append the data to the time series and then plot it.

K = addsample(TS, DATA(:,2));
plot(K);

But doing this produced the following error:

A new sample must be specified either as a structure or by Property-Value pairs.

So how do I plot this time data correctly? Thank you!

I work quite often with posix time (ie unixtime ) with other programs, but in matlab the easiest format to deal with time and date is the Matlab time serial number format.

To convert from Unix to Matlab I use a small conversion function extensively:

function matlabtime = unix2matlabtime(unixtime)
%// function matlabtime = unix2matlabtime(unixtime)
%//
%// input : ** unixtime ** : time vector in the UNIX  time serial number
%//                          representation (seconds since 01-jan-1970)
%//
%// output : ** matlabtime **  : time vector in the Matlab time serial number
%//                          representation (days since 01-jan-0000)

pivot = datenum([1970 01 01 00 00 00]) ;
matlabtime = ( unixtime / 24 / 3600 ) + pivot ;

with this function saved somewhere on your path, you can plot your data like so:

%// Generate sample data
sampleunixtime = linspace( 1427205640 , 1427205900 ).' ;         %'// ignore this comment
DATA = [sampleunixtime , sin(sampleunixtime./10) ]  ;

%// get a time vector in Matlab time serial format, then 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:

imgexample

look at the datenum and datestr documentation to see how to handle these. There are many predefined output format for the date/time or you can even build your own to refine to the precision you need (add millisecond, remove seconds, add the date etc ...).

Just be aware that the XTickLabel are now overriden, so they will not be updated automatically. So if you zoom or pan on your figure you'll have to re-run the last line of code to refresh the XTicks values.

( personally, I put this last line of code in a Matlab toolbar shortcut to have quick access to it at any time ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM