00001 function ht = plotsign2 (h,s,Opt);
00002 % ht = plotsign2 (h,s,[Opt]);
00003 % h is the figure handle, if you need to specify a subplot within the figure
00004 % pass the figure handle and the subplot number in h, like: [1 213]
00005 % or [1 2 1 3]
00006 % s is the text string, if s is not supplied, a default string is used
00007 % the default is : machine : date \n path:filename of calling matlab script
00008 %
00009 % Opt is an optional structure with the following optional fields
00010 % .Place is an approximate position for placing the text.
00011 % .Place is a three character string where the first character
00012 % specifies whether the placement is relative to the Figure 'F'
00013 % or the plot 'P'. The second character
00014 % specifies the Y position, and the second character the
00015 % X position. For example 'FTC' means Figure Top Center.
00016 % Your options for the relative positioning are :
00017 % F for figure and P for plot
00018 % Your options for the Y position (first letter) are:
00019 % T or C or B (Top or Center or Bottom)
00020 % Your options for the X position (second letter) are:
00021 % L or C or R (Left or Center or Right)
00022 % text alignment is done automatically, unless you specify a .Align option.
00023 % The default is 'FBR'
00024 % .Pos (2x1 or 3x1 vector) pins the X Y postion (relative the X Y axis) of the plot
00025 % .Pos overrides .Place.
00026 % NOTE : The units of .Pos are relative to the plot
00027 % .Font is the fontsize to use (a number or string). (like 8, or 10 or 12 etc).
00028 % The default is 2 numbers less the current axis on the figure (usually 8)
00029 % You can also specify 'l' or 's' for large or small (2 less or 2 more than
00030 % current axis). 'n' for a font equal to that of current axis.
00031 % .FontName is an optional string specifying a font name
00032 % .Color : choose colors that you normally specify in text function, like 'r'
00033 % the default is 'k'. You can also specify an rgb vector
00034 % (values between 1 and 0)
00035 % .HAlign : string specifying the horizontal alignment of text, options are
00036 % 'left', 'center', 'right', '' for default
00037 % .VAlign : string specifying the vertical alignment of text, options are
00038 % 'top', 'middle', 'bottom', '' for default
00039 %
00040 % Once you see the object, you can move it with the mouse
00041 % ht is the handle to the text object
00042 %
00043 % example:
00044 % t=0:0.1:50; Y = sin(0.5.*t); cf = figure(1); plot (t,Y);
00045 % Ssign = sprintf ('Z.S.Saad %s\n image:%s/%s', date, pwd, mfilename );
00046 % plotsign2(cf,Ssign);
00047 % or
00048 % Opt.Place = 'FTC'; Opt.Color = 'r'; Opt.Font = 16;
00049 % plotsign2(cf,Ssign,Opt);
00050 % or
00051 % Opt.Pos = [t(400) Y(400)];Opt.Color = 'b'; Opt.Font = 12;
00052 % plotsign2(cf,Ssign,Opt);
00053 %
00054 % See also
00055 % MoveFigObject
00056 % HistoryTrace
00057 % Ziad Saad Dec 1 97, latest fix Wed May 05 21:41:30 CDT 1999
00058 %
00059
00060 ht = 0; %initialize in case you return early
00061
00062 figure (h(1));
00063 if (length(h) > 1),
00064 if (length(h) == 2), subplot (h(2)); %in case you want to specify a subplot
00065 elseif (length(h) == 4), subplot (h(2), h(3), h(4));
00066 else err = ErrEval(FuncName,'Err_Bad size for input parameter h');
00067 return;
00068 end
00069 end
00070
00071
00072 if (nargin == 1),
00073 [ST,I] = dbstack;
00074 if (length(ST) > 1), %use the calling function name as a signature
00075 [jnk1, CallPath, CallFname] = GetPath(ST(2).name);
00076 else %this function is called directly from command line
00077 CallPath = pwd;
00078 CallFname = mfilename;
00079 end
00080 [tmp, smach] = unix('hostname');
00081 %remove this annoying tset message (some bug ....)
00082 [err, snl, Nlines] = GetNextLine(smach, 2);
00083 if (Nlines >= 2),
00084 [err, smach] = GetNextLine(smach,Nlines);
00085 end
00086 if (tmp),
00087 smach = sprintf('Dunno');
00088 else
00089 smach = deblank(smach); smach = smach(1:length(smach)-1);
00090 end
00091 c=datevec(now);
00092 s = sprintf ('Ziad Saad SSCC/NIMH/NIH\n%s : %s %s:%s:%s\n%s/%s', smach, date,...
00093 pad_strn(num2str(c(4)), '0', 2, 1),...
00094 pad_strn(num2str(c(5)), '0', 2, 1),...
00095 pad_strn(num2str(round(c(6))), '0', 2, 1),...
00096 CallPath, CallFname);
00097 Opt.Place = 'FBR';
00098 elseif (nargin == 2),
00099 Opt.Place = 'FBR';
00100 end
00101
00102 if (~isfield(Opt,'Place') | isempty(Opt.Place)),
00103 Opt.Place = 'FBR';
00104 end
00105 if (~isfield(Opt,'HAlign')),
00106 Opt.HAlign = '';
00107 end
00108 if (~isfield(Opt,'VAlign')),
00109 Opt.VAlign = '';
00110 end
00111 if (~isfield(Opt,'Position')),
00112 Opt.Position = [];
00113 end
00114
00115 if (~isfield(Opt,'Pos') | isempty(Opt.Pos)),
00116 spec = 0;
00117 else
00118 if (length(Opt.Pos) ~= 2 & length(Opt.Pos) ~= 3),
00119 ErrEval(mfilename,'Err_Bad size for Opt.Pos parameter');
00120 return;
00121 end
00122 spec = 1;
00123 end
00124
00125 if (~isfield(Opt,'Color') | isempty(Opt.Color)),
00126 Opt.Color = 'k';
00127 end
00128
00129 % Fontsize for text
00130 if (~isfield(Opt,'Font') | isempty(Opt.Font)),
00131 Opt.Font = 's';
00132 end
00133 if (ischar(Opt.Font)),
00134 if (eq_str(Opt.Font,'l')),
00135 fs = get(gcf,'defaultaxesfontsize')+2;
00136 elseif (eq_str(Opt.Font,'s')),
00137 fs = get(gcf,'defaultaxesfontsize')-2;
00138 elseif (eq_str(Opt.Font,'n')),
00139 fs = get(gcf,'defaultaxesfontsize');
00140 end
00141 else
00142 fs = Opt.Font;
00143 end
00144
00145
00146
00147 %place text anywhere
00148 ht=text(0,0,s, 'color',Opt.Color);
00149 %set fonts
00150 if (isfield(Opt,'FontName') & ~isempty(Opt.FontName)),
00151 set(ht,'FontName', Opt.FontName);
00152 end
00153 %set font size
00154 set(ht,'fontsize',fs);
00155 %set interpreter
00156 set(ht,'Interpreter','none');
00157
00158 %store current units
00159 tmpunt = get(ht,'Units');
00160
00161
00162 %set position and alignment as fit
00163 if (spec), %user specified location
00164 set(ht,'Units','data');
00165 set(ht,'Position',Opt.Pos);
00166
00167 %override auto-alignment if specified
00168 if (~isempty(Opt.VAlign)), AlgnV = Opt.VAlign;
00169 else AlgnV = 'middle'; end
00170 if (~isempty(Opt.HAlign)), AlgnH = Opt.HAlign;
00171 else AlgnH = 'center'; end
00172
00173 set(ht,'horizontalalignment',AlgnH);
00174 set(ht,'verticalalignment',AlgnV);
00175 else %auto placement
00176 %get the size in normalized units of the axes
00177 UntAxes = get(gca,'Units');
00178 if (~eq_str(UntAxes,'normalized')),
00179 ErrEval(mfilename,'Err_Sorry, must have normalized units');
00180 return;
00181 end
00182
00183 AxSize = get(gca,'Position');
00184 %if the axes does not fill the figure window, find out what would fill it
00185 Mult = [1./AxSize(3) 1./AxSize(4)];
00186
00187 Vpos = [0 0];
00188 Unt = 'normalized';
00189 switch Opt.Place(1), %relative to figure or plot
00190 case 'F', %figure
00191 DoF = 1;
00192 case 'P', %plot
00193 DoF = 0;
00194 otherwise,
00195 ErrEval(mfilename,'Err_Cannot interpret Opt.Place(1)');
00196 return;
00197 end
00198 switch Opt.Place(2), %the Y axis
00199 case 'B', %bottom
00200 if (DoF),
00201 Vpos(2) = (-AxSize(2) + 0.02) .* Mult(2);
00202 AlgnV = 'bottom';
00203 else Vpos(2) = (-0.05);
00204 AlgnV = 'top';
00205 end
00206
00207 case 'C', %center
00208 if (DoF), Vpos(2) = (-AxSize(2) + 0.5 + 0.02) .* Mult(2);
00209 else Vpos(2) = 0.5; end
00210 AlgnV = 'middle';
00211
00212 case 'T',
00213 if (DoF), Vpos(2) = (-AxSize(2) - 0.02) .* Mult(2) + Mult(2) ;
00214 else Vpos(2) = 0.95; end
00215 AlgnV = 'top';
00216 otherwise,
00217 ErrEval(mfilename,'Err_Cannot interpret Opt.Place(2)');
00218 return;
00219 end
00220 switch Opt.Place(3), %the X axis
00221 case 'L', %bottom
00222 if (DoF), Vpos(1) = (-AxSize(1) + 0.02) .* Mult(1);
00223 else Vpos(1) = (-0.05); end
00224 AlgnH = 'left';
00225 case 'C', %center
00226 if (DoF), Vpos(1) = (-AxSize(1) + 0.5 + 0.02).* Mult(1);
00227 else Vpos(1) = 0.5; end
00228 AlgnH = 'center';
00229 case 'R',
00230 if (DoF), Vpos(1) = (-AxSize(1) - 0.02) .* Mult(1) + Mult(1);
00231 else Vpos(1) = 0.95; end
00232 AlgnH = 'right';
00233 otherwise,
00234 ErrEval(mfilename,'Err_Cannot interpret Opt.Place(3)');
00235 return;
00236 end
00237
00238 %override auto-alignment if specified
00239 if (~isempty(Opt.VAlign)), AlgnV = Opt.VAlign; end
00240 if (~isempty(Opt.HAlign)), AlgnH = Opt.HAlign; end
00241
00242 set(ht,'horizontalalignment',AlgnH);
00243 set(ht,'verticalalignment',AlgnV);
00244 set(ht,'Units',Unt);
00245 set(ht,'Position',Vpos);
00246 end
00247
00248 set(ht,'Units',tmpunt);
00249
00250 %Now you need to store the handle to the object and give it a number
00251 %By giving each object a number, you can store multiple objects on the same
00252 %figure and manipulate them all individually
00253
00254 %get the UserData of the figure
00255 ud = get (gcf,'UserData');
00256 %make sure there has been handle stored previously, otherwise add some
00257 if (~isfield(ud,'MaxHandle') | isempty(ud.MaxHandle)),
00258 ud.MaxHandle = 0;
00259 end
00260 %Increment the maximum number of handles
00261 ud.MaxHandle = ud.MaxHandle + 1;
00262 %Now add the handle in the MoveHandle vector
00263 ud.MoveHandle(ud.MaxHandle) = ht;
00264 %store the userdata back in the figure
00265 set(gcf,'UserData',ud);
00266 %create the button callback string
00267 stmp = sprintf('MoveFigObject(%g)',ud.MaxHandle);
00268 %set the button down function
00269 set(ht,'ButtonDownFcn',stmp);
00270
00271 return;