Home > bioelectromagnetism > eeg_plot.m

eeg_plot

PURPOSE ^

eeg_plot - create a report style plot of ERP data

SYNOPSIS ^

function Fig = eeg_plot(time,erp,scale,interval,width,titles)

DESCRIPTION ^

 eeg_plot - create a report style plot of ERP data

 Usage: eeg_plot(time,erp,scale,interval,width,titles)
 
 time  -  an Mx1 column vector of times (in msec) for each row of erp
 
 erp   -  an MxN matrix, M rows of time points, N channels
 
 scale -  a 1x4 row vector = [xmin,xmax,ymin,ymax], the
          defaults are: min(time), max(time)
                        -/+ max(max(abs(erp)))
 
 interval - a 1x2 row vector = [xint, yint], to specify the
            x,y tick mark intervals (defaults are 100 msec and 1 uV)
 
 width -  1 = journal page width (default, 15.8 cm)
          0 = journal column width (7.4 cm)
 
          plot height is (2/3) * width
 
 titles   - a text struct with (default) fields:
 
            titles.title  - figure title ('')
            titles.xlabel - x label ('msec')
            titles.xalign - text alignment ('center')
            titles.xxpos  - position of x label relative to x axis ([])
            titles.xypos  - position of x label relative to y axis ([])
            titles.ylabel - y label ('\muV', tex for uV)
            titles.yalign - text alignment ('center')
            titles.yypos  - position of y label relative to y axis ([])
            titles.yxpos  - position of y label relative to x axis ([])
 
 The figure may appear small on screen, but it is formatted to
 print in either the width of a journal page (15.8 cm) or the
 width of a journal column (7.5 cm).
 To adjust it to your preferred dimensions, hack the code, which is
 well documented.  If you want different colours or all black lines,
 hack the code at the plot command. Alternatively, use
 a graphics package to convert specific colours or convert all
 colours to greyscale.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function Fig = eeg_plot(time,erp,scale,interval,width,titles)
0002 
0003 % eeg_plot - create a report style plot of ERP data
0004 %
0005 % Usage: eeg_plot(time,erp,scale,interval,width,titles)
0006 %
0007 % time  -  an Mx1 column vector of times (in msec) for each row of erp
0008 %
0009 % erp   -  an MxN matrix, M rows of time points, N channels
0010 %
0011 % scale -  a 1x4 row vector = [xmin,xmax,ymin,ymax], the
0012 %          defaults are: min(time), max(time)
0013 %                        -/+ max(max(abs(erp)))
0014 %
0015 % interval - a 1x2 row vector = [xint, yint], to specify the
0016 %            x,y tick mark intervals (defaults are 100 msec and 1 uV)
0017 %
0018 % width -  1 = journal page width (default, 15.8 cm)
0019 %          0 = journal column width (7.4 cm)
0020 %
0021 %          plot height is (2/3) * width
0022 %
0023 % titles   - a text struct with (default) fields:
0024 %
0025 %            titles.title  - figure title ('')
0026 %            titles.xlabel - x label ('msec')
0027 %            titles.xalign - text alignment ('center')
0028 %            titles.xxpos  - position of x label relative to x axis ([])
0029 %            titles.xypos  - position of x label relative to y axis ([])
0030 %            titles.ylabel - y label ('\muV', tex for uV)
0031 %            titles.yalign - text alignment ('center')
0032 %            titles.yypos  - position of y label relative to y axis ([])
0033 %            titles.yxpos  - position of y label relative to x axis ([])
0034 %
0035 % The figure may appear small on screen, but it is formatted to
0036 % print in either the width of a journal page (15.8 cm) or the
0037 % width of a journal column (7.5 cm).
0038 % To adjust it to your preferred dimensions, hack the code, which is
0039 % well documented.  If you want different colours or all black lines,
0040 % hack the code at the plot command. Alternatively, use
0041 % a graphics package to convert specific colours or convert all
0042 % colours to greyscale.
0043 %
0044 
0045 % $Revision: 1.3 $ $Date: 2005/05/23 21:25:52 $
0046 
0047 % Licence:  GNU GPL, no express or implied warranties
0048 % History:  07/2000, Darren.Weber_at_radiology.ucsf.edu
0049 %                    Adapted from (c) Drea Thomas, June 1995
0050 %                                  MathWorks newsletter
0051 %
0052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0053 
0054 % Requirements:
0055 % Grids
0056 % Legend on top of diagram
0057 
0058 if ~exist('interval','var'), interval = []; end
0059 
0060 
0061 
0062 % ----- Set the figure dimensions and scaling -----
0063 
0064 if ~exist('width','var'),
0065     % Width of a journal page = 15.8 cm
0066     width = 15.8;
0067     fontsize = 8;
0068     ylabeloffset = 0.1;
0069 elseif isempty(width),
0070     % Width of a journal page = 15.8 cm
0071     width = 15.8;
0072     fontsize = 8;
0073     ylabeloffset = 0.1;
0074 elseif width,
0075     % Width of a journal page = 15.8 cm
0076     width = 15.8;
0077     fontsize = 8;
0078     ylabeloffset = 0.1;
0079 else
0080     % Width of a journal column = 7.4 cm
0081     width = 7.4;
0082     fontsize = 6;
0083     ylabeloffset = 0.1;
0084 end
0085 
0086 height = (2/3) * width;
0087 
0088 % If you want to control exactly how a plot is going to look on paper,
0089 % it is a good idea to WYSIWYG (what you see is what you get) the figure
0090 % window. That is, set the figure window 'Units' property to physical
0091 % units, and set the figure window size the same as the 'PaperPosition'
0092 % size. For our case,
0093 Fig = figure;
0094 set(Fig,'PaperUnits','centimeters','PaperPosition',[1 1 width height]) 
0095 set(Fig,'Units','centimeters','position',get(Fig,'PaperPosition'))
0096 movegui(Fig,'center');
0097 % Note that the lower left-hand corner of the graph is offset by 1 cm
0098 % in X and Y because most printers can't physically print to the edge of
0099 % a piece of paper. If you specified a [0 0] offset, the plot would be
0100 % cut off.
0101 
0102 
0103 
0104 % ----- Set default fonts -----
0105 
0106 % Journals require a letter size no smaller than approx. 2 mm after
0107 % reduction of the figure.
0108 % Font sizes are usually measured in points. Since there are about
0109 % 2.8 points per mm, the minimum fontsize should be 6 or 7.  Let's
0110 % set the default fontsize for the figure window to 8.
0111 set(Fig,'DefaultAxesFontSize',fontsize)
0112 set(Fig,'DefaultAxesFontName','Arial')
0113 % See Drea's Desk, April 1994 for a discussion of default properties
0114 % (ftp://ftp.mathworks.com/pub/doc/tmw-digest/apr94).
0115 
0116 
0117 
0118 % ----- plot the data -----
0119 
0120 %plot(time,erp,'k'); % black lines
0121 
0122 if size(erp,2) == 1,
0123     plot(time,erp(:,1),'r-', 'linewidth',1.0);
0124 elseif size(erp,2) == 2,
0125     plot(time,erp(:,1),'r-', 'linewidth',1.0); hold on;
0126     plot(time,erp(:,2),'b--','linewidth',0.8);
0127 else
0128     plot(time,erp,'linewidth',0.8); % msec
0129     %plot(time./1000,erp,'linewidth',0.5); % convert to sec
0130 end
0131 
0132 
0133 
0134 % ----- Set the axis properties -----
0135 
0136 make_scale = 0;
0137 if ~exist('scale','var'), make_scale = 1; end
0138 if isempty(scale), make_scale = 1; end
0139 if make_scale,
0140     %xmin = min(time) / 1000; % convert to sec
0141     %xmax = max(time) / 1000;
0142     
0143     xmin = min(time); % msec
0144     xmax = max(time);
0145     
0146     elecmax = max(abs(erp));
0147     ymax = max(elecmax);
0148     ymin = -1 * ymax;
0149     
0150     scale = [xmin,xmax,ymin,ymax];
0151 else
0152     xmin = scale(1);
0153     xmax = scale(2);
0154     ymin = scale(3);
0155     ymax = scale(4);
0156 end
0157 if ~exist('interval','var'),
0158     interval(1) = 100; % msec
0159     interval(2) = (ymax - ymin) / 10;
0160     %interval = [0.1 1]; % sec
0161 end
0162 
0163 axis(scale);
0164 
0165 Ax = gca;
0166 set(Ax,'YDir','reverse');
0167 set(Ax,'Box','off');
0168 
0169 set(Ax,'XGrid','on');
0170 if ~isempty(interval),
0171     set(Ax,'XTickLabelMode','manual');
0172     set(Ax,'XTickMode','manual');
0173     set(Ax,'XTick',     [xmin:interval(1):xmax]);
0174     set(Ax,'XTickLabel',[xmin:interval(1):xmax]);
0175 end
0176 set(Ax,'YGrid','on');
0177 if ~isempty(interval),
0178     set(Ax,'YTickLabelMode','manual');
0179     set(Ax,'YTickMode','manual');
0180     set(Ax,'YTick'     ,[ymin:interval(2):ymax]);
0181     set(Ax,'YTickLabel',[ymin:interval(2):ymax]);
0182 end
0183 
0184 
0185 % ----- Reposition the axis -----
0186 
0187 % The default axis size is too large (the labels get cut off).
0188 % Hence, modify the size and location of the axis within the
0189 % figure window,
0190 pos = get(Ax,'position');      % This is in normalized coordinates
0191 pos(1) = pos(1)+.01;           % Shift axes left by a factor of .01
0192 pos(2) = pos(2)+.02;           % Shift axes up by a factor of .02
0193 pos(3) = pos(3) + pos(3)*.05;  % Stretch x axis by a factor of .05
0194 set(Ax,'position',pos);
0195 
0196 
0197 
0198 % ----- Add titles -----
0199 
0200 make_title = 0;
0201 if ~exist('titles','var'), make_title = 1; end
0202 if isempty(titles), make_title = 1; end
0203 if make_title,
0204     titles.title  = '';
0205     titles.xlabel = 'msec';
0206     titles.xalign = 'center';
0207     titles.xxpos  = [];
0208     titles.xypos  = [];
0209     titles.ylabel = '\muV';
0210     titles.yalign = 'center';
0211     titles.yrot   = 0;
0212     titles.yypos  = [];
0213     titles.yxpos  = [];
0214 end
0215 
0216 if ~isfield(titles,'title'),  titles.title  = ''; end
0217 if ~isfield(titles,'xlabel'), titles.xlabel = 'msec'; end
0218 if ~isfield(titles,'xalign'), titles.xalign = 'center'; end
0219 if ~isfield(titles,'xxpos'),  titles.xxpos  = []; end
0220 if ~isfield(titles,'xypos'),  titles.xypos  = []; end
0221 
0222 if ~isfield(titles,'ylabel'), titles.ylabel = '\muV'; end
0223 if ~isfield(titles,'yalign'), titles.yalign = 'center'; end
0224 if ~isfield(titles,'yrot'),   titles.yrot = 0; end
0225 if ~isfield(titles,'yypos'),  titles.yypos  = []; end
0226 if ~isfield(titles,'yxpos'),  titles.yxpos  = []; end
0227 
0228 title(titles.title);
0229 Hx = xlabel(titles.xlabel,'Rotation',0,'HorizontalAlignment',titles.xalign);
0230 Hy = ylabel(titles.ylabel,'Rotation',titles.yrot,'HorizontalAlignment',titles.yalign);
0231 Yextent = get(Hy,'extent');
0232 % Yextent is the position and size of text. A four-element read-only
0233 % vector that defines the size and position of the text string.
0234 % [left,bottom,width,height]
0235 % If the Units property is set to data (the default), left and bottom
0236 % are the x and y coordinates of the lower-left corner of the text
0237 % Extent rectangle.  For all other values of Units, left and bottom
0238 % are the distance from the lower-left corner of the axes position
0239 % rectangle to the lower-left corner of the text Extent rectangle.
0240 % width and height are the dimensions of the Extent rectangle. All
0241 % measurements are in units specified by the Units property.
0242 if titles.xxpos,
0243     xxoffset = titles.xxpos;
0244 else
0245     pos = get(Hx,'Position');
0246     xxoffset = pos(1);
0247 end
0248 if titles.xypos,
0249     xyoffset = titles.xypos;
0250 else
0251     pos = get(Hx,'Position');
0252     xyoffset = pos(2);
0253 end
0254 set(Hx,'Position',[xxoffset xyoffset]);
0255 
0256 if titles.yypos,  yyoffset = titles.yypos;
0257 else              yyoffset = Yextent(4) / 4;
0258 end
0259 if titles.yxpos,  yxoffset = titles.yxpos;
0260 else
0261     pos = get(Hy,'Position');
0262     yxoffset = pos(1) + ( pos(1) * 0.1 );
0263 end
0264 set(Hy,'Position',[yxoffset yyoffset]);
0265 
0266 
0267 
0268 % ----- Now for the legend -----
0269 
0270 % h = legend('Sin','Cos');
0271 % axes(h);
0272 % refresh;
0273 
0274 % Legend is an axis. Unless it is the current axes, it is
0275 % drawn "behind" the other axis (i.e., your plot). So, just
0276 % before you print, make it the current axis and it will
0277 % appear "on top" (you wouldn't want to always make it the
0278 % current axis because then the next PLOT command would go
0279 % into the legend).

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003