diff options
Diffstat (limited to 'sharedfiles')
-rw-r--r-- | sharedfiles/dispmat.m | 51 | ||||
-rw-r--r-- | sharedfiles/fact.m | 10 | ||||
-rw-r--r-- | sharedfiles/figtext.m | 61 | ||||
-rw-r--r-- | sharedfiles/figtext3D.m | 71 | ||||
-rw-r--r-- | sharedfiles/plothorline.m | 101 | ||||
-rw-r--r-- | sharedfiles/plotvertline.m | 102 | ||||
-rw-r--r-- | sharedfiles/reverse.m | 5 | ||||
-rw-r--r-- | sharedfiles/scalemarker.m | 32 |
8 files changed, 433 insertions, 0 deletions
diff --git a/sharedfiles/dispmat.m b/sharedfiles/dispmat.m new file mode 100644 index 0000000..8b57818 --- /dev/null +++ b/sharedfiles/dispmat.m @@ -0,0 +1,51 @@ +%---------------------------------------------------------------
+% Displays a numeric matrix in a more readable format than
+% the one with which Matlab normally displays matrixes.
+% If filename is specified, then the matrix is written to
+% a text file. If specified, the format can be controlled
+% via the format argument which has the same form as the
+% FORMAT argument in sprintf.
+%
+% SYNTAX: dispmat(matrix <,filename,format,writeflag>);
+%
+%---------------------------------------------------------------
+% SYNTAX: dispmat(matrix<,format,filename,writeflag>);
+%---------------------------------------------------------------
+
+function dispmat(matrix,varargin);
+
+errorstate=0;
+if nargin==1
+ disp(num2str(matrix));
+end
+if nargin>=4, writeflag=varargin{3}; else writeflag='w'; end
+
+if nargin>=3
+ filename=varargin{2};
+ fid=fopen(filename,writeflag);
+ if ~isempty(varargin{1})
+ format=varargin{1};
+ for s=1:size(matrix,1)
+ matrow=num2str(matrix(s,:),format);
+ fprintf(fid,'%s\n',matrow);
+ end
+ else
+ for s=1:size(matrix,1)
+ matrow=num2str(matrix(s,:));
+ fprintf(fid,'%s\n',matrow);
+ end
+ end
+ fclose(fid);
+ errorstate=fid;
+end
+
+if nargin==2
+ format=varargin{1};
+ for s=1:size(matrix,1)
+ matrow=num2str(matrix(s,:),format);
+ fprintf('%s\n',matrow);
+ end
+ end
+end
+
+
diff --git a/sharedfiles/fact.m b/sharedfiles/fact.m new file mode 100644 index 0000000..b7f7cb2 --- /dev/null +++ b/sharedfiles/fact.m @@ -0,0 +1,10 @@ +% Vector form of factorial.
+% For some reason the function "factorial" can't act on vectors.
+% 6/24/03
+
+function y=fact(x);
+
+y=[];
+for s=1:length(x)
+ y(s)=factorial(x(s));
+end
\ No newline at end of file diff --git a/sharedfiles/figtext.m b/sharedfiles/figtext.m new file mode 100644 index 0000000..e828f16 --- /dev/null +++ b/sharedfiles/figtext.m @@ -0,0 +1,61 @@ +%---------------------------------------------------------------
+% PROGRAM: figtext
+% DATE: 4/15/03
+% AUTHOR: Andri M. Gretarsson
+%
+% SYNTAX: [xcoord,ycoord,h]=figtext(x,y,textstring <,fontsize>);
+% or h=figtext3D(x,y,textstring <,fontsize>);
+%
+% arguments in <...> are optional
+%
+% Writes the text in the string textstring to the current figure
+% at the location given by x,y. The location is specified on a
+% 10x10 grid with x=0, y=0 corresponding to the lower left hand
+% corner of the figure. The font size of the text can be optionally
+% specified (in points) by supplying the argument fontsize.
+% [xcoord,ycoord] returns the value of the axes coordinates at
+% the position specified by x,y. The third output argument h, is
+% the handle to the text written to the figure.
+%
+% Last updated: 4/15/03 by AMG
+%
+%----------------------------------------------------------------
+% SYNTAX: [xcoord,ycoord,h]=figtext(x,y,textstring <,fontsize>);
+% or h=figtext(x,y,textstring <,fontsize>);
+%----------------------------------------------------------------
+
+function varargout=figtext(x,y,textstring,varargin);
+
+xlim=get(gca,'XLim');
+ylim=get(gca,'Ylim');
+xlen=xlim(2)-xlim(1);
+ylen=ylim(2)-ylim(1);
+ndiv=10;
+
+xscale=get(gca,'XScale');
+yscale=get(gca,'YScale');
+
+if xscale(1:3)=='log'
+ xcoord=10^(log10(xlim(1))+x/ndiv*(log10(xlim(2))-log10(xlim(1))));
+else
+ xcoord=xlim(1)+x/ndiv*xlen;
+end
+if yscale(1:3)=='log'
+ ycoord=10^(log10(ylim(1))+y/ndiv*(log10(ylim(2))-log10(ylim(1))));
+else
+ ycoord=ylim(1)+y/ndiv*ylen;
+end
+
+if nargin>=4
+ h=text(xcoord,ycoord,textstring,'FontSize',varargin{1});
+else
+ h=text(xcoord,ycoord,textstring);
+end
+
+if nargout>1
+ varargout{1}=xcoord;
+ varargout{2}=ycoord;
+ varargout{4}=h;
+else
+ varargout{1}=h;
+end
\ No newline at end of file diff --git a/sharedfiles/figtext3D.m b/sharedfiles/figtext3D.m new file mode 100644 index 0000000..a905704 --- /dev/null +++ b/sharedfiles/figtext3D.m @@ -0,0 +1,71 @@ +% A function for writing text to 3D graphs in a simple way.
+%--------------------------------------------------------------------------
+% PROGRAM: figtext
+% DATE: 4/15/03
+% AUTHOR: Andri M. Gretarsson
+%
+% SYNTAX: [xcoord,ycoord,zcoord,h]=figtext3D(x,y,z,textstring <,fontsize>);
+% or h=figtext3D(x,y,z,textstring <,fontsize>);
+%
+% arguments in <...> are optional
+%
+% Writes the text in the string textstring to the current figure
+% at the location given by x,y. The location is specified on a
+% 10x10 grid with x=0, y=0 corresponding to the lower left hand
+% corner of the figure. The font size of the text can be optionally
+% specified (in points) by supplying the argument fontsize.
+% [xcoord,ycoord] returns the value of the axes coordinates at
+% the position specified by x,y. The third output argument h, is
+% the handle to the text written to the figure.
+%
+% Last updated: 4/15/03 by AMG
+%
+%---------------------------------------------------------------------------
+% SYNTAX: [xcoord,ycoord,zcoord,h]=figtext3D(x,y,z,textstring <,fontsize>);
+% or h=figtext3D(x,y,z,textstring <,fontsize>);
+%---------------------------------------------------------------------------
+
+function varargout=figtext3D(x,y,z,textstring,varargin);
+
+xlim=get(gca,'XLim');
+ylim=get(gca,'Ylim');
+zlim=get(gca,'Zlim');
+xlen=xlim(2)-xlim(1);
+ylen=ylim(2)-ylim(1);
+zlen=zlim(2)-zlim(1);
+ndiv=10;
+
+xscale=get(gca,'XScale');
+yscale=get(gca,'YScale');
+zscale=get(gca,'ZScale');
+
+if xscale(1:3)=='log'
+ xcoord=10^(log10(xlim(1))+x/ndiv*(log10(xlim(2))-log10(xlim(1))));
+else
+ xcoord=xlim(1)+x/ndiv*xlen;
+end
+if yscale(1:3)=='log'
+ ycoord=10^(log10(ylim(1))+y/ndiv*(log10(ylim(2))-log10(ylim(1))));
+else
+ ycoord=ylim(1)+y/ndiv*ylen;
+end
+if zscale(1:3)=='log'
+ zcoord=10^(log10(zlim(1))+z/ndiv*(log10(zlim(2))-log10(zlim(1))));
+else
+ zcoord=zlim(1)+z/ndiv*zlen;
+end
+
+if nargin>=5
+ h=text(xcoord,ycoord,zcoord,textstring,'FontSize',varargin{1});
+else
+ h=text(xcoord,ycoord,zcoord,textstring);
+end
+
+if nargout>1
+ varargout{1}=xcoord;
+ varargout{2}=ycoord;
+ varargout{3}=zcoord;
+ varargout{4}=h;
+else
+ varargout{1}=h;
+end
\ No newline at end of file diff --git a/sharedfiles/plothorline.m b/sharedfiles/plothorline.m new file mode 100644 index 0000000..ea9f9d6 --- /dev/null +++ b/sharedfiles/plothorline.m @@ -0,0 +1,101 @@ +%------------------------------------------------------------------------------
+% PROGRAM NAME: Plot horizontal lines
+% FILE: plotvertline.m
+% AUTHOR: Andri M. Gretarsson
+% DATE: 02/03/03
+%
+% SYNTAX: linehandles=plothorline(y,xmin,xmax <,marker,plotstyle>)
+%
+% Plots horizontal lines at the positions listed in the vector y. The vertical
+% lines extend from xmin to xmax and are ploted with the markerstyle and
+% color defined by marker if specified (same syntax as the marker in matlab's
+% built in function plot. Exits with hold set to off. If plotstyle is specified
+% then the plotstyle will be linear-linear, lin-log, log-lin or log-log,
+% depending on whether plotstyle is 'plot', 'semilogx', 'semilogy', or 'loglog'.
+%
+% If 'marker' is a string whose first character is an apostrophe ('),
+% the marker string in interpreted literally as part of the command, as in:
+% eval(['plot([xmin xmax],[y(i) y(i)],',marker,')']);
+%
+% If the first character of marker is not an apostrophe, the marker string is
+% just inserted into the command as usual without using eval, namely:
+% plot([xmin xmax],[y(i) y(i)],marker).
+%
+% Thus, for example:
+% marker = '''b-'',''LineWidth'',2' results in the plot command
+% plot(x,y,'b-','LineWidth',2) being evaluated.
+%
+% marker = 'b-' has the same effect as marker = '''b-''', namely
+% plot(x,y,'b-'), in the first case directly via plot(x,y,'b-') and
+% in the second case via eval(['plot(x,y,',marker,')']).
+%
+% If xmin=0 and xmax=0, then ymin and ymax are obtained from the current
+% axes.
+%
+% LAST UPDATED: 03/13/03
+%
+%-------------------------------------------------------------------------------
+% SYNTAX: linehandles=plothorline(y,xmin,xmax <,marker,plotstyle>)
+%-------------------------------------------------------------------------------
+
+function linehandles=plothorline(y,xmin,xmax,varargin);
+
+if nargin>=4, marker=varargin{1}; else marker='b-'; end
+if nargin>=5,
+ plotstyle=varargin{2};
+else plotstyle='plot';
+end
+if xmin==0 & xmax==0
+ range=get(gca,'XLim');
+ xmin=range(1);
+ xmax=range(2);
+end
+
+NextPlot_current=get(gca,'NextPlot'); % Get the original 'hold' state
+
+h=zeros(length(y),1);
+
+if plotstyle(1)=='p'
+ for i=1:length(y)
+ if marker(1)==''''
+ eval(['h(i)=plot([xmin xmax],[y(i) y(i)],',marker,')']); hold on;
+ else
+ h(i)=plot([xmin xmax],[y(i) y(i)],marker); hold on;
+ end
+ end
+else
+if plotstyle(1)=='l'
+ for i=1:length(y)
+ if marker(1)==''''
+ eval(['h(i)=loglog([xmin xmax],[y(i) y(i)],',marker,')']); hold on;
+ else
+ h(i)=loglog([xmin xmax],[y(i) y(i)],marker); hold on;
+ end
+ end
+else
+if (length(plotstyle)==8 &plotstyle(8)=='x')
+ for i=1:length(y)
+ if marker(1)==''''
+ eval(['h(i)=semilogx([xmin xmax],[y(i) y(i)],',marker,')']); hold on;
+ else
+ h(i)=semilogx([xmin xmax],[y(i) y(i)],marker); hold on;
+ end
+ end
+else
+if (length(plotstyle)==8 &plotstyle(8)=='y')
+ for i=1:length(y)
+ if marker(1)==''''
+ eval(['h(i)=semilogy([xmin xmax],[y(i) y(i)],',marker,')']); hold on;
+ else
+ h(i)=semilogy([xmin xmax],[y(i) y(i)],marker); hold on;
+ end
+ end
+end;end;end;end;
+
+if NextPlot_current(1)=='r' % reset to the original 'hold' state
+ set(gca,'NextPlot','replace')
+else
+ set(gca,'NextPlot','add');
+end
+
+linehandles=h;
\ No newline at end of file diff --git a/sharedfiles/plotvertline.m b/sharedfiles/plotvertline.m new file mode 100644 index 0000000..aff512a --- /dev/null +++ b/sharedfiles/plotvertline.m @@ -0,0 +1,102 @@ +%------------------------------------------------------------------------------
+% PROGRAM NAME: Plot vertical lines
+% FILE: plotvertline.m
+% AUTHOR: Andri M. Gretarsson
+% DATE: 02/03/03
+%
+% SYNTAX: linehandles=plotvertline(x,ymin,ymax <,marker,plotstyle>)
+%
+% Plots vertical lines at the positions listed in the vector x. The vertical
+% lines extend from ymin to ymax and are ploted with the markerstyle and
+% color defined by marker if specified (same syntax as the marker in matlab's
+% built in function plot. Exits with hold set to off. If plotstyle is specified
+% then the plotstyle will be linear-linear, lin-log, log-lin or log-log,
+% depending on whether plotstyle is 'plot', 'semilogx', 'semilogy', or 'loglog'.
+%
+% If 'marker' is a string whose first character is an apostrophe ('),
+% the marker string in interpreted literally as part of the command, as in:
+% eval(['plot([xmin xmax],[y(i) y(i)],',marker,')']);
+%
+% If the first character of marker is not an apostrophe, the marker string is
+% just inserted into the command as usual without using eval, namely:
+% plot([xmin xmax],[y(i) y(i)],marker).
+%
+% Thus, for example:
+% marker = '''b-'',''LineWidth'',2' results in the plot command
+% plot(x,y,'b-','LineWidth',2) being evaluated.
+%
+% marker = 'b-' has the same effect as marker = '''b-''', namely
+% plot(x,y,'b-'), in the first case directly via plot(x,y,'b-') and
+% in the second case via eval(['plot(x,y,',marker,')']).
+%
+% If ymin=0 and ymax=0, then ymin and ymax are obtained from the current
+% axes, and the function draws a line from the bottom to the top of the
+% current graph.
+%
+% LAST UPDATED: 03/13/03
+%
+%-------------------------------------------------------------------------------
+% SYNTAX: linehandles=plotvertline(y,xmin,xmax <,marker,plotstyle>)
+%-------------------------------------------------------------------------------
+
+function linehandles=plotvertline(x,ymin,ymax,varargin);
+
+if nargin>=4, marker=varargin{1}; else marker='b-'; end
+if nargin>=5,
+ plotstyle=varargin{2};
+else plotstyle='plot';
+end
+if ymin==0 & ymax==0
+ range=get(gca,'YLim');
+ ymin=range(1);
+ ymax=range(2);
+end
+
+NextPlot_current=get(gca,'NextPlot'); % Get the original 'hold' state
+
+h=zeros(length(x),1);
+
+if plotstyle(1)=='p'
+ for i=1:length(x)
+ if marker(1)==''''
+ eval(['h(i)=plot([x(i) x(i)],[ymin ymax],',marker,')']); hold on;
+ else
+ h(i)=plot([x(i) x(i)],[ymin ymax],marker); hold on;
+ end
+ end
+else
+if plotstyle(1)=='l'
+ for i=1:length(x)
+ if marker(1)==''''
+ eval(['h(i)=loglog([x(i) x(i)],[ymin ymax],',marker,')']); hold on;
+ else
+ h(i)=loglog([x(i) x(i)],[ymin ymax],marker); hold on;
+ end
+ end
+else
+if (length(plotstyle)==8 &plotstyle(8)=='x')
+ for i=1:length(x)
+ if marker(1)==''''
+ eval(['h(i)=semilogx([x(i) x(i)],[ymin ymax],',marker,')']); hold on;
+ else
+ h(i)=semilogx([x(i) x(i)],[ymin ymax],marker); hold on;
+ end
+ end
+else
+if (length(plotstyle)==8 &plotstyle(8)=='y')
+ for i=1:length(x)
+ if marker(1)==''''
+ eval(['h(i)=semilogy([x(i) x(i)],[ymin ymax],',marker,')']); hold on;
+ else
+ h(i)=semilogy([x(i) x(i)],[ymin ymax],marker); hold on;
+ end
+ end
+end;end;end;end;
+
+if NextPlot_current(1)=='r' % reset to the original 'hold' state
+ set(gca,'NextPlot','replace')
+else
+ set(gca,'NextPlot','add');
+end
+
+linehandles=h;
\ No newline at end of file diff --git a/sharedfiles/reverse.m b/sharedfiles/reverse.m new file mode 100644 index 0000000..010529b --- /dev/null +++ b/sharedfiles/reverse.m @@ -0,0 +1,5 @@ +% Reverses the positions of the elements of a vector
+
+function r=reverse(v);
+
+r=v([length(v):-1:1]);
\ No newline at end of file diff --git a/sharedfiles/scalemarker.m b/sharedfiles/scalemarker.m new file mode 100644 index 0000000..88556cb --- /dev/null +++ b/sharedfiles/scalemarker.m @@ -0,0 +1,32 @@ +% Puts a scale marker ("scale bar") on a plot at position (x,y). The length of the
+% marker is set by markerlength. The string textlabel usually contains the
+% "real life" length the marker is supposed to represent. The final optional
+% argument is the orientation. It should be 'h' for horizontal (default) or 'v'
+% for vertical.
+
+function [h,th]=scalemarker(x,y,markerlength,textlabel,varargin);
+
+
+if nargin>=5, orientation=varargin{1}; else orientation='h'; end
+
+xlimits=get(gca,'XLim');
+ylimits=get(gca,'YLim');
+xrange=abs(diff(xlimits));
+yrange=abs(diff(ylimits));
+
+if orientation=='h'
+ x1=x-markerlength/2;
+ x2=x+markerlength/2;
+ y1=y-yrange/90;
+ y2=y+yrange/90;
+ h=plot([x1 x2],[y y],'k-',[x1 x1],[y1 y2],'k-',[x2 x2],[y1 y2],'k-');
+ th=text(x1,y2+yrange/75,[' ',textlabel]);
+else
+ x1=x-xrange/120;
+ x2=x+xrange/120;
+ y1=y-markerlength/2;
+ y2=y+markerlength/2;
+ h=plot([x x],[y1 y2],'k-',[x1 x2],[y1 y1],'k-',[x1 x2],[y2 y2],'k-');
+ th=text(x2,y1,strvcat(textlabel,' ',' '));
+end
+
|