ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
getAxes.m
Go to the documentation of this file.
1%> \brief
2%> Create axes in the specified input tiled position and return its handle.<br>
3%>
4%> \param[in] varargin : Any attributes of the axes, which can be one of the following positional arguments:<br>
5%> <ol>
6%> <li> ``rows `` : The input scalar MATLAB whole number,
7%> representing the number of rows of subplots in the figure.<br>
8%> <li> ``cols `` : The input scalar MATLAB whole number,
9%> representing the number of columns of subplots in the figure.<br>
10%> <li> ``cellx`` : The input scalar MATLAB whole number,
11%> representing the row at which the new axes must be placed in the figure.<br>
12%> <li> ``celly`` : The input scalar MATLAB whole number,
13%> representing the column at which the new axes must be placed in the figure.<br>
14%> <li> ``spanx`` : The input scalar MATLAB whole number in the range ``[0, 1]``,
15%> representing the X-span of the current axes in the figure.<br>
16%> <li> ``spany`` : The input scalar MATLAB whole number in the range ``[0, 1]``,
17%> representing the Y-span of the current axes in the figure.<br>
18%> </ol>
19%> or specified as pairs of scalar ``char``,
20%> followed by the specifications value in the range ``[0, 1]``.<br>
21%> The following attributes are acceptable:<br>
22%> <ol>
23%> <li> ``'Spacing'`` , also ``'s'``
24%> <li> ``'SpacingHoriz'`` , also ``'sh'``
25%> <li> ``'SpacingVert'`` , also ``'sv'``
26%> <li> ``'Padding'`` , also ``'p'``
27%> <li> ``'PaddingRight'`` , also ``'pr'``
28%> <li> ``'PaddingLeft'`` , also ``'pl'``
29%> <li> ``'PaddingTop'`` , also ``'pt'``
30%> <li> ``'PaddingBottom'`` , also ``'pb'``
31%> <li> ``'Margin'`` , also ``'m'``
32%> <li> ``'MarginRight'`` , also ``'mr'``
33%> <li> ``'MarginLeft'`` , also ``'ml'``
34%> <li> ``'MarginTop'`` , also ``'mt'``
35%> <li> ``'MarginBottom'`` , also ``'mb'``
36%> <li> ``'Holdaxis'`` , also ``'h'`` : If ``true``, any axes below the current will not be deleted.
37%> </ol>
38%> (**optional**. If missing, a fedault value will be used.)
39%>
40%> \interface{getAxes}
41%> \code{.m}
42%>
43%> h = getAxes(rows, cols, cellno, varargin)
44%> h = getAxes(rows, cols, cellx, celly, varargin)
45%> h = getAxes(rows, cols, cellx, celly, spanx, spany, varargin)
46%>
47%> \endcode
48%>
49%> \example{getAxes}
50%> \include{lineno} example/vis/axes/getAxes/main.m
51%> \vis{getAxes}
52%> \image html example/vis/axes/getAxes/getAxes.png width=700
53%>
54%> \final{getAxes}
55%>
56%> This code builds upon the works of 2001-2014 / Aslak Grinsted.<br>
57%>
58%> \author
59%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
60%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
61function h = getAxes(varargin)
62 f = gcf;
63 userDataArgsOK = 0;
64 args = get(f, 'UserData');
65 if isstruct(args)
66 userDataArgsOK = isfield(args, 'SpacingHorizontal') & isfield(args, 'Holdaxis') & isfield(args, 'rows') & isfield(args, 'cols');
67 end
68 OKToStoreArgs = isempty(args) | userDataArgsOK;
69 if isempty(args) && (~userDataArgsOK)
70 args = struct('Holdaxis',0, ...
71 'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
72 'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
73 'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
74 'rows',[],'cols',[]);
75 end
76 args = parseArgs(varargin,args,{'Holdaxis'},{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
77 if (length(args.NumericArguments)>2)
78 args.rows=args.NumericArguments{1};
79 args.cols=args.NumericArguments{2};
80 %remove these 2 numerical arguments
81 args.NumericArguments={args.NumericArguments{3:end}};
82 end
83 if OKToStoreArgs
84 set(f,'UserData',args);
85 end
86 switch length(args.NumericArguments)
87 case 0
88 return % no arguments but rows/cols....
89 case 1
90 if numel(args.NumericArguments{1}) > 1 % restore subplot(m,n,[x y]) behaviour
91 [x1 y1] = ind2sub([args.cols args.rows],args.NumericArguments{1}(1)); % subplot and ind2sub count differently (column instead of row first) --> switch cols/rows
92 [x2 y2] = ind2sub([args.cols args.rows],args.NumericArguments{1}(end));
93 else
94 x1=mod((args.NumericArguments{1}-1),args.cols)+1; x2=x1;
95 y1=floor((args.NumericArguments{1}-1)/args.cols)+1; y2=y1;
96 end
97 % x1=mod((args.NumericArguments{1}-1),args.cols)+1; x2=x1;
98 % y1=floor((args.NumericArguments{1}-1)/args.cols)+1; y2=y1;
99 case 2
100 x1=args.NumericArguments{1};x2=x1;
101 y1=args.NumericArguments{2};y2=y1;
102 case 4
103 x1=args.NumericArguments{1};x2=x1+args.NumericArguments{3}-1;
104 y1=args.NumericArguments{2};y2=y1+args.NumericArguments{4}-1;
105 otherwise
106 error('getAxes argument error')
107 end
108
109 cellwidth=((1-args.MarginLeft-args.MarginRight)-(args.cols-1)*args.SpacingHorizontal)/args.cols;
110 cellheight=((1-args.MarginTop-args.MarginBottom)-(args.rows-1)*args.SpacingVertical)/args.rows;
111 xpos1=args.MarginLeft+args.PaddingLeft+cellwidth*(x1-1)+args.SpacingHorizontal*(x1-1);
112 xpos2=args.MarginLeft-args.PaddingRight+cellwidth*x2+args.SpacingHorizontal*(x2-1);
113 ypos1=args.MarginTop+args.PaddingTop+cellheight*(y1-1)+args.SpacingVertical*(y1-1);
114 ypos2=args.MarginTop-args.PaddingBottom+cellheight*y2+args.SpacingVertical*(y2-1);
115 if args.Holdaxis
116 h=axes('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
117 else
118 h=subplot('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
119 end
120 set(h,'box','on');
121 %h=axes('position',[x1 1-y2 x2-x1 y2-y1]);
122 set(h,'units',get(gcf,'defaultaxesunits'));
123 set(h,'tag','getAxes');
124 if (nargout==0), clear h; end;
125end
126
127%> \cond excluded
128
129function argStruct = parseArgs(args, argStruct, varargin)
130 % Helper function for parsing varargin.
131 %
132 % argStruct=parseArgs(varargin,argStruct[,FlagtypeParams[,Aliases]])
133 %
134 % * argStruct is the structure full of named arguments with default values.
135 % * Flagtype params is params that don't require a value. (the value will be set to 1 if it is present)
136 % * Aliases can be used to map one argument-name to several argstruct fields
137 %
138 % example usage:
139 % --------------
140 % function parseargtest(varargin)
141 %
142 % %define the acceptable named arguments and assign default values
143 % args=struct('Holdaxis',0, ...
144 % 'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
145 % 'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
146 % 'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
147 % 'rows',[],'cols',[]);
148 %
149 % %The capital letters define abrreviations.
150 % % Eg. parseargtest('spacingvertical',0) is equivalent to parseargtest('sv',0)
151 %
152 % args=parseArgs(varargin,args, ... % fill the arg-struct with values entered by the user
153 % {'Holdaxis'}, ... %this argument has no value (flag-type)
154 % {'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
155 %
156 % disp(args)
157 %
158 %
159 %
160 %
161 % Aslak Grinsted 2004
162 % -------------------------------------------------------------------------
163 % Copyright (C) 2002-2004, Aslak Grinsted
164 % This software may be used, copied, or redistributed as long as it is not
165 % sold and this copyright notice is reproduced on each copy made. This
166 % routine is provided as is without any express or implied warranties
167 % whatsoever.
168 persistent matlabver
169 if isempty(matlabver)
170 matlabver=ver('MATLAB');
171 matlabver=str2double(matlabver.Version);
172 end
173 Aliases={};
174 FlagTypeParams='';
175 if (length(varargin)>0)
176 FlagTypeParams=lower(strvcat(varargin{1})); %#ok
177 if length(varargin)>1
178 Aliases=varargin{2};
179 end
180 end
181
182 %---------------Get "numeric" arguments
183 NumArgCount=1;
184 while (NumArgCount<=size(args,2))&&(~ischar(args{NumArgCount}))
185 NumArgCount=NumArgCount+1;
186 end
187 NumArgCount=NumArgCount-1;
188 if (NumArgCount>0)
189 argStruct.NumericArguments={args{1:NumArgCount}};
190 else
191 argStruct.NumericArguments={};
192 end
193 %--------------Make an accepted fieldname matrix (case insensitive)
194 Fnames=fieldnames(argStruct);
195 for i=1:length(Fnames)
196 name=lower(Fnames{i,1});
197 Fnames{i,2}=name; %col2=lower
198 Fnames{i,3}=[name(Fnames{i,1}~=name) ' ']; %col3=abreviation letters (those that are uppercase in the argStruct) e.g. SpacingHoriz->sh
199 %the space prevents strvcat from removing empty lines
200 Fnames{i,4}=isempty(strmatch(Fnames{i,2},FlagTypeParams)); %Does this parameter have a value?
201 end
202 FnamesFull=strvcat(Fnames{:,2}); %#ok
203 FnamesAbbr=strvcat(Fnames{:,3}); %#ok
204 if length(Aliases)>0
205 for i=1:length(Aliases)
206 name=lower(Aliases{i,1});
207 FieldIdx=strmatch(name,FnamesAbbr,'exact'); %try abbreviations (must be exact)
208 if isempty(FieldIdx)
209 FieldIdx=strmatch(name,FnamesFull); %&??????? exact or not?
210 end
211 Aliases{i,2}=FieldIdx;
212 Aliases{i,3}=[name(Aliases{i,1}~=name) ' ']; %the space prevents strvcat from removing empty lines
213 Aliases{i,1}=name; %dont need the name in uppercase anymore for aliases
214 end
215 %Append aliases to the end of FnamesFull and FnamesAbbr
216 FnamesFull=strvcat(FnamesFull,strvcat(Aliases{:,1})); %#ok
217 FnamesAbbr=strvcat(FnamesAbbr,strvcat(Aliases{:,3})); %#ok
218 end
219 %--------------get parameters--------------------
220 l=NumArgCount+1;
221 while (l<=length(args))
222 a=args{l};
223 if ischar(a)
224 paramHasValue=1; % assume that the parameter has is of type 'param',value
225 a=lower(a);
226 FieldIdx=strmatch(a,FnamesAbbr,'exact'); %try abbreviations (must be exact)
227 if isempty(FieldIdx)
228 FieldIdx=strmatch(a,FnamesFull);
229 end
230 if (length(FieldIdx)>1) %shortest fieldname should win
231 [mx,mxi]=max(sum(FnamesFull(FieldIdx,:)==' ',2));%#ok
232 FieldIdx=FieldIdx(mxi);
233 end
234 if FieldIdx>length(Fnames) %then it's an alias type.
235 FieldIdx=Aliases{FieldIdx-length(Fnames),2};
236 end
237
238 if isempty(FieldIdx)
239 error(['Unknown named parameter: ' a])
240 end
241 for curField=FieldIdx' %if it is an alias it could be more than one.
242 if (Fnames{curField,4})
243 if (l+1>length(args))
244 error(['Expected a value for parameter: ' Fnames{curField,1}])
245 end
246 val=args{l+1};
247 else %FLAG PARAMETER
248 if (l<length(args)) %there might be a explicitly specified value for the flag
249 val=args{l+1};
250 if isnumeric(val)
251 if (numel(val)==1)
252 val=logical(val);
253 else
254 error(['Invalid value for flag-parameter: ' Fnames{curField,1}])
255 end
256 else
257 val=true;
258 paramHasValue=0;
259 end
260 else
261 val=true;
262 paramHasValue=0;
263 end
264 end
265 if matlabver>=6
266 argStruct.(Fnames{curField,1})=val; %try the line below if you get an error here
267 else
268 argStruct=setfield(argStruct,Fnames{curField,1},val); %#ok <-works in old matlab versions
269 end
270 end
271 l=l+1+paramHasValue; %if a wildcard matches more than one
272 else
273 error(['Expected a named parameter: ' num2str(a)])
274 end
275 end
276end
277
278%> \endcond
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared libraries.
function copy(in from, in to, in fields)
Copy the contents of the struct/object from to the struct/object to recursively and without destroyin...
function getAxes(in varargin)
Create axes in the specified input tiled position and return its handle.
function isnumeric(in str)
Return a scalar MATLAB logical that is true if and only if the input string can be converted to a sca...
function map()
Return a scalar MATLAB logical that is true if and only if the current installation of MATLAB contain...
excluded
Definition: show.m:173
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...
function win()
Return true if the current OS is Windows.