function [dataout,varargout]=plotXmatrix(data,varargin) % % [dataout,h]=plotMatrix(data,varargin) % % USAGE: % [dataout,L]=plotMatrix(data); % [dataout,L]=plotMatrix(data,t); % [dataout,L]=plotMatrix(data,'align') % [dataout,L]=plotMatrix(data,t,'align') % % If only 1 output is specified, then dataout (a matrix the same size as % data) is returned. The order of t and 'align' is immaterial, as long as % the data matrix is input first. % % INPUT: % data: a matrix of data vectors stored column-wise. % t: an optional vector the same length as the columns of data. The data % are plotted against t, so that is on the x axis. % 'align': A string option that aligns the data according to the maximum of % cross-correlation between data(:,1) and the other columns. % % OUTPUT: % dataout: A matrix size(data) back. If 'align' is used, then dataout's % columns are shifted with respect to data's columns, so that the % time series line up. % L: Optional. A vector of lag indices. These give the amount the column % vectors of dataout are shifted with respect to the first column of % data. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [M,N]=size(data); datascale=max(max(abs(data))); lagout=zeros(N,1); colors={'k','r','b'}; if(datascale~=0) dataout=data./datascale; end; if(datascale==0) dataout=data; end; %dataout=detrend(dataout); if(nargin==3) % OBTAIN ALIGN OPTION if(ischar(varargin{1})) doalign=strcmp(varargin{1},'align'); if(doalign==0) sprintf('Undefined character option'); end; if(doalign==1) t=varargin{2}; plot(t,dataout(:,1),'k'); hold on; for(k=1:N-1) x1=dataout(:,1); x2=dataout(:,k+1); [C,lags]=xcorr(x1,x2,'coeff'); [maxC,I]=max(abs(C)); if(nargout==2); lagout(k+1)=lags(I); end; x2=circshift(x2,lags(I)); dataout(:,k+1)=x2; plot(t,dataout(:,k+1)+k,char(colors(mod(k,3)+1))); hold on; axis tight; end; end; end; if(ischar(varargin{2})) doalign=strcmp(varargin{2},'align'); if(doalign==0) sprintf('Undefined character option'); end; if(doalign==1) t=varargin{1}; plot(t,dataout(:,1),char(colors(mod(k,3)+1))); hold on; for(k=1:N-1) x1=dataout(:,1); x2=dataout(:,k+1); [C,lags]=xcorr(x1,x2,'coeff'); [maxC,I]=max(abs(C)); if(nargout==2); lagout(k+1)=lags(I); end; x2=circshift(x2,lags(I)); dataout(:,k+1)=x2; plot(t,dataout(:,k+1)+k,char(colors(mod(k,3)+1))); hold on; axis tight; end; end; end; end; if(nargin==2); % CHECK FOR ALIGN OPTION if(ischar(varargin{1})) doalign=strcmp(varargin{1},'align'); if(doalign==0) sprintf('Undefined character option'); end; if(doalign==1) plot(dataout(:,1),'k'); hold on; for(k=1:N-1) x1=dataout(:,1); x2=dataout(:,k+1); [C,lags]=xcorr(x1,x2,'coeff'); [maxC,I]=max(abs(C)); if(nargout==2); lagout(k+1)=lags(I); end; x2=circshift(x2,lags(I)); dataout(:,k+1)=x2; plot(dataout(:,k+1)+k,char(colors(mod(k,3)+1))); hold on; axis tight; end; end; end; % CHECK FOR INDEPENDENT VARIABLE if(isnumeric(varargin{1})) t=varargin{1}; plot(t,dataout(:,1),'k'); hold on; for(k=1:N-1) plot(t,dataout(:,k+1)+k,char(colors(mod(k,3)+1))); hold on; axis tight; end; end; end; if(nargin==1); plot(dataout(:,1),'k'); hold on; for(k=1:N-1) plot(dataout(:,k+1)+k,char(colors(mod(k,3)+1))); hold on; axis tight; end; end; if(nargout==2); varargout{1}=lagout; end;