-
Notifications
You must be signed in to change notification settings - Fork 1
/
sublimeFigure.m
663 lines (576 loc) · 28.9 KB
/
sublimeFigure.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
classdef sublimeFigure < handle
%sublimeFigure Easily control plot & subplot layout, figure print size.
% Requires MATLAB r2017a.
% Create figures with multiple subplots of the right size for
% publications. Layout is based on a rectangular matrix, where each
% subplot can take one or any number of cells. Define exact size to
% match your publication, font sizes, set precise margins in cm (in).
% Default properties create a square figure with one axis (plot) of the
% width suitable for one-column-wide figure in an APS publication.
%
% Designed to run in scripts, not tested with manual
% resizing in GUI, apps etc.
%
% Version: 1.2 (2018-01-03)
% (C) 2018, M. Sich, The University of Sheffield
properties (SetObservable)
% Total width of the figure (default 8.6 cm ).
totalWidth(1,1) double {mustBeNonnegative} = 8.6
% Total height of the figure (default 8.6 cm).
totalHeight(1,1) double {mustBeNonnegative} = 8.6
%
leftOuterPadding(1,1) double {mustBeNonnegative} = 1.1
%
rightOuterPadding(1,1) double {mustBeNonnegative} = 0.1
% single number or 1D array with individual values for each column
leftPadding(:,1) double {mustBeNonnegative} = 0
% single number or 1D array with individual values for each column
rightPadding(:,1) double {mustBeNonnegative} = 0
%
topOuterPadding(1,1) double {mustBeNonnegative} = 0.1
%
bottomOuterPadding(1,1) double {mustBeNonnegative}= 1.0
% single number or 1D array with individual values for each row
topPadding(:,1) double {mustBeNonnegative} = 0
% single number or 1D array with individual values for each row
bottomPadding(:,1) double {mustBeNonnegative} = 0
% Number of columns for unit cell array
numColumns(1,1) double {mustBeNonnegative} = 1
% Number of rows for unit cell array
numRows(1,1) double {mustBeNonnegative} = 1
% 1D array of relative heights of columns, by default is ones.
rowHeightWeights(:,1) double {mustBeNonnegative,...
mustBeLessThanOrEqual(rowHeightWeights,1)} = 1
% 1D array of relative widths of rows, by default is ones.
colWidthWeights(:,1) double {mustBeNonnegative,...
mustBeLessThanOrEqual(colWidthWeights,1)} = 1
% Default font size for labelling features
defFontSize(1,1) double {mustBePositive} = 8
% Colour-bar width (default 0.2 cm)
cBarWidth(1,1) double {mustBeNonnegative} = 0.2
% Colour-bar padding from the edge of the plot (default 0.1 cm)
cBarPadding(1,1) double {mustBeNonnegative} = 0.1
end
properties (SetObservable, AbortSet)
% Use AbortSet = true to trigger callback listener only if the
% value of the property has changed, so that assigning the same
% value twice will only trigger call back on the first instance.
% Default units of all physical dimensions. Can be either 'cm' or
% 'in'.
defUnits(1,:) char {mustBeMember(defUnits,{'cm','in'})} = 'cm'
end
% ====================================================================
properties ( SetAccess = private )
% Figure handle
fig
end
% ====================================================================
properties ( SetAccess = private, Hidden = true )
% Calculated automatically
lop(1,1) double
rop(1,1) double
lp(:,1) double
rp(:,1) double
cellWidth(1,1) double
top(1,1) double
bop(1,1) double
tp(:,1) double
bp(:,1) double
cellHeight(1,1) double
% structure of subplots' (axis) handles and props: ax, col, row, cSpan,
% rSpan. Empty by default.
sfPlots = []
propChangeListeners = event.listener.empty % array of handles to listeners
end
% ====================================================================
% ====================================================================
methods
function obj = sublimeFigure( varargin )
% Create a figure based on default or passed in a preset values
% The first and only expected input is the preset string
if nargin > 0
preset = varargin{1};
else
% If nothing passed through then set to 'default'
preset = 'default';
end
% Validate input
validatestring( preset, {'default', 'presentation', 'tight',...
'sparse'} );
% Do different settings for different presets
switch preset
case 'presentation'
obj.totalWidth = 15;
obj.totalHeight = 12;
obj.defFontSize = 16;
obj.cBarWidth = 0.5;
obj.cBarPadding = 0.2;
obj.bottomOuterPadding = 2.1;
obj.leftOuterPadding = 2.9;
case 'tight'
obj.leftPadding = 0.05;
obj.rightPadding = 0.05;
obj.topPadding = 0.05;
obj.bottomPadding = 0.05;
obj.topOuterPadding = 0.05;
obj.rightOuterPadding = 0.05;
case 'sparse'
obj.leftPadding = 1.1;
obj.rightPadding = 0;
obj.topPadding = 0.1;
obj.bottomPadding = 1.0;
obj.topOuterPadding = 0;
obj.rightOuterPadding = 0.1;
obj.leftOuterPadding = 0;
obj.bottomOuterPadding = 0;
case 'default'
% do nothing
otherwise
% Just doublechecking
error( 'sublimeFigure: Unknown preset "%s"!', preset);
end
% Set sizes
obj.setRelativeSizes();
% Creating figure object and setting paper sizes
obj.fig = figure;
% Need to change default settings since whenever something is
% plotted in axis, it resets font size...
set( obj.fig, 'DefaultAxesFontSize', obj.defFontSize);
set( obj.fig, 'DefaultAxesLabelFontSizeMultiplier', 1 );
set( obj.fig, 'DefaultAxesTitleFontSizeMultiplier', 1 );
% Update figure
obj.resizeFigure;
% Adding listeners for all public property changes to update
% figure after any change.
% Get meta data from the current object instance.
mco = metaclass(obj);
% Get an array of struct containing info on all public
% properties.
plist = mco.PropertyList;
for i = 1 : length(plist)
% Add a listener only if the property can be 'listened'.
% All of those impact figure sizing, so run resizeFigure in
% each case.
if plist(i).SetObservable == true
if string(plist(i).Name) == 'defUnits'
% Only if units are changed call a recalculate
% function instead of resize. Otherwise the actual
% figure size have changed.
% NB: Name property of a meta.class object is char
% array, not a string!
obj.propChangeListeners(end+1) = addlistener(...
obj, plist(i).Name,'PostSet', @obj.changeUnits);
else
obj.propChangeListeners(end+1) = addlistener(...
obj, plist(i).Name,'PostSet', @obj.resizeFigure);
end
end
end
end
% ================================================================
function delete(~)
% Destructor
end
% ================================================================
function [ ax, axID ] = subPlot( obj, cCol, cRow, varargin )
%(cCol,cRow) creates a subplot at unit cell at cCol column and
% cRow row. Returns axes handle. Optional cSpan and rSpan in
% varargin to account for having a multi-cell plot. By default
% these are set to 1.
% Validating inputs
validateattributes( cCol, {'numeric'},...
{'integer', 'nonnegative'});
validateattributes( cRow, {'numeric'},...
{'integer', 'nonnegative'});
if cCol > obj.numColumns
warnTxt = ['sublimeFigure warns: attempting to create'...
'a subplot starting at ' int2str(cCol) ' column may result'...
' in unexpected figure appearance. Max columns is set'...
' to ' int2str(obj.numColumns) ];
warning( warnTxt );
end
if cRow > obj.numRows
warnTxt = ['sublimeFigure warns: attempting to create'...
'a subplot starting at ' int2str(cRow) ' row may result'...
' in unexpected figure appearance. Max rows is set'...
' to ' int2str(obj.numRows) ];
warning( warnTxt );
end
if nargin == 5
cSpan = varargin{1};
rSpan = varargin{2};
validateattributes( cSpan, {'numeric'},...
{'integer', 'nonnegative'});
validateattributes( rSpan, {'numeric'},...
{'integer', 'nonnegative'});
if ( cCol + cSpan - 1) > obj.numColumns
warnTxt = ['sublimeFigure warns: attempting to create'...
'a subplot starting at ' int2str(cCol) ' column and '...
'spanning over ' int2str(cSpan) ' columns may result'...
' in unexpected figure appearance. Max. columns '...
' is set to ' int2str(obj.numColumns) ];
error( warnTxt );
end
if ( cRow + rSpan - 1 ) > obj.numRows
warnTxt = ['sublimeFigure warns: attempting to create'...
'a subplot starting at ' int2str(cRow) ' row and '...
'spanning over ' int2str(rSpan) ' rows may result'...
' in unexpected figure appearance. Max. rows is '...
'set to ' int2str(obj.numRows) ];
error( warnTxt );
end
else
rSpan = 1;
cSpan = 1;
end
% Calculate size and position
plotBottom = obj.getPlotBottom( cRow, rSpan );
plotLeft = obj.getPlotLeft( cCol );
plotWidth = obj.getPlotWidth( cCol, cSpan );
plotHeight = obj.getPlotHeight( cRow, rSpan );
% Create new axis
ax = subplot('Position',[plotLeft, plotBottom, plotWidth, plotHeight]);
% Add the newly created plot to the structure containing
% handles and basic properties of all subplots
axID = length( obj.sfPlots ) + 1;
obj.sfPlots( axID ).ax = ax;
obj.sfPlots( axID ).row = cRow;
obj.sfPlots( axID ).col = cCol;
obj.sfPlots( axID ).rSpan = rSpan;
obj.sfPlots( axID ).cSpan = cSpan;
end
% ================================================================
function cb = colorbar( obj, axID )
% Add a colourbar to axis with axID. Returns handle to colourbar
validateattributes( axID, {'numeric'},...
{'integer', 'nonnegative', '<=', length(obj.sfPlots)});
% Get the handle of current axis to set back focus to it once
% the colour bar is created, in the case axID is not the same
% as the current axis
currAx = gca;
% Change focus to the axID
axes( obj.sfPlots( axID ).ax );
% Add colour bar
cb = colorbar;
cbLeft = obj.getPlotLeft( obj.sfPlots(axID).col ) +...
obj.cBarPadding/obj.totalWidth +...
obj.getPlotWidth( obj.sfPlots(axID).col, obj.sfPlots(axID).cSpan );
cb.Position = [ cbLeft,...
obj.getPlotBottom( obj.sfPlots(axID).row, obj.sfPlots(axID).rSpan ),...
obj.cBarWidth/obj.totalWidth,...
obj.getPlotHeight( obj.sfPlots(axID).row, obj.sfPlots(axID).rSpan )];
cb.FontSize = obj.defFontSize;
% Reset current axis
axes( currAx );
end
% ================================================================
function lb = label( obj, axID, location, str )
% Add a neat text label to the subplot in either 'topleft',
% 'topright', 'bottomleft', or 'bottomright' corners.
validatestring( location, {'topleft', 'topright',...
'bottomleft', 'bottomright'} );
validateattributes( axID, {'numeric'},...
{'integer', 'nonnegative', '<=', length(obj.sfPlots)});
% Default distance of the text from the edges of the specified
% corner
dx = 0.1;
dy = 0.1;
% Get the handle of current axis to set back focus to it once
% the colour bar is created, in the case axID is not the same
% as the current axis
currAx = gca;
% Change focus to the axID
axes( obj.sfPlots( axID ).ax );
% Add text label
lb = text( 'String', str );
lb.FontSize = obj.defFontSize;
% Record default units
defAxUnits = obj.sfPlots( axID ).ax.Units;
defLbUnits = lb.Units;
% Position the label
switch obj.defUnits
case 'cm'
lb.Units = 'centimeters';
obj.sfPlots( axID ).ax.Units = 'centimeters';
axPC = obj.sfPlots( axID ).ax.Position;
% get the size of the letter from pt. 1 pt = 1/72 in, 1
% inch = 2.54 cm....
lbHeight = 1/72 * obj.defFontSize * 2.54;
case 'in'
lb.Units = 'inches';
obj.sfPlots( axID ).ax.Units = 'inches';
axPC = obj.sfPlots( axID ).ax.Position;
% get the size of the letter from pt. 1 pt = 1/72 in, 1
% inch = 2.54 cm....
lbHeight = 1/72 * obj.defFontSize;
end
switch location
case 'topleft'
lb.HorizontalAlignment = 'left';
pos = [ dx, axPC(4) - dy - lbHeight/2 ];
case 'bottomleft'
lb.HorizontalAlignment = 'left';
pos = [ dx, dy + lbHeight/2 ];
case 'topright'
lb.HorizontalAlignment = 'right';
pos = [ axPC(3) - dx, axPC(4) - dy - lbHeight/2 ];
case 'bottomright'
lb.HorizontalAlignment = 'right';
pos = [ axPC(3) - dx, dy + lbHeight/2 ];
otherwise
error( 'sublimeFigure screams: Unknown label location "%s"!', location);
end
lb.Position = pos;
% Reset units back for resizing to work properly later
obj.sfPlots( axID ).ax.Units = defAxUnits;
lb.Units = defLbUnits;
% Reset axis
axes( currAx );
end
end
% ====================================================================
% ====================================================================
methods ( Access = private, Hidden = true )
function obj = changeUnits( obj, varargin )
% Change from in to cm and back. The function is triggered
% 'post set', so new value of defUnits is the target one.
%Disable property change listeners
for i = 1 : length(obj.propChangeListeners)
obj.propChangeListeners(i).Enabled = false;
end
switch obj.defUnits
case 'cm'
% Switch from in to cm
obj.totalWidth = obj.totalWidth * 2.54;
obj.totalHeight = obj.totalHeight * 2.54;
obj.leftOuterPadding = obj.leftOuterPadding * 2.54;
obj.rightOuterPadding = obj.rightOuterPadding * 2.54;
obj.leftPadding = obj.leftPadding * 2.54;
obj.rightPadding = obj.rightPadding * 2.54;
obj.topOuterPadding = obj.topOuterPadding * 2.54;
obj.bottomOuterPadding = obj.bottomOuterPadding * 2.54;
obj.topPadding = obj.topPadding * 2.54;
obj.bottomPadding = obj.bottomPadding * 2.54;
obj.cBarWidth = obj.cBarWidth * 2.54;
obj.cBarPadding = obj.cBarPadding * 2.54;
case 'in'
% Switch from cm to in
obj.totalWidth = obj.totalWidth / 2.54;
obj.totalHeight = obj.totalHeight / 2.54;
obj.leftOuterPadding = obj.leftOuterPadding / 2.54;
obj.rightOuterPadding = obj.rightOuterPadding / 2.54;
obj.leftPadding = obj.leftPadding / 2.54;
obj.rightPadding = obj.rightPadding / 2.54;
obj.topOuterPadding = obj.topOuterPadding / 2.54;
obj.bottomOuterPadding = obj.bottomOuterPadding / 2.54;
obj.topPadding = obj.topPadding / 2.54;
obj.bottomPadding = obj.bottomPadding / 2.54;
obj.cBarWidth = obj.cBarWidth / 2.54;
obj.cBarPadding = obj.cBarPadding / 2.54;
otherwise
error( ['Unknown unit type: ', obj.defUnits,...
'. Must be either in or cm.'] );
end
%Enable property change listeners back
for i = 1 : length(obj.propChangeListeners)
obj.propChangeListeners(i).Enabled = true;
end
end
function obj = resizeFigure( obj, varargin )
% Resize figure according to the new/updated values
switch obj.defUnits
case 'cm'
% Setting paper sizes
obj.fig.PaperUnits = 'centimeters';
% PaperPosition for raster images
obj.fig.PaperPosition = [0 0 obj.totalWidth obj.totalHeight];
% PaperSize is used by full page pdf and PostScript printers
obj.fig.PaperSize = [obj.totalWidth obj.totalHeight];
obj.fig.PaperPositionMode = 'manual';
% Getting same proportion on screen
% Sets the units of the root object (screen) to pixels
set(0,'units','pixels');
% Obtains this pixel information
ss.px = get(0,'screensize');
% Sets the units of the root object (screen) to cm
set(0,'units','centimeters');
% Obtains this inch information
ss.cm = get(0,'screensize');
% Calculates the resolution (pixels per cm)
ss.res = ss.px ./ ss.cm;
% Resizing the onscreen figure
obj.fig.Units = 'pixels';
obj.fig.Position(3) = round( obj.totalWidth * ss.res(3) );
obj.fig.Position(4) = round( obj.totalHeight * ss.res(4) );
obj.fig.Position(1) = round( 0.5 * ( ss.px(3)-obj.fig.Position(3)));
obj.fig.Position(2) = round( 0.5 * ( ss.px(4)-obj.fig.Position(4)));
case 'in'
% Setting paper sizes
obj.fig.PaperUnits = 'inches';
% PaperPosition for raster images
obj.fig.PaperPosition = [0 0 obj.totalWidth obj.totalHeight];
% PaperSize is used by full page pdf and PostScript printers
obj.fig.PaperSize = [obj.totalWidth obj.totalHeight];
obj.fig.PaperPositionMode = 'manual';
% Getting same proportion on screen
% Sets the units of the root object (screen) to pixels
set(0,'units','pixels');
% Obtains this pixel information
ss.px = get(0,'screensize');
% Sets the units of the root object (screen) to cm
set(0,'units','inches');
% Obtains this inch information
ss.in = get(0,'screensize');
% Calculates the resolution (pixels per cm)
ss.res = ss.px ./ ss.in;
% Resizing the onscreen figure
obj.fig.Units = 'pixels';
obj.fig.Position(3) = round( obj.totalWidth * ss.res(3) );
obj.fig.Position(4) = round( obj.totalHeight * ss.res(4) );
obj.fig.Position(1) = round( 0.5 * ( ss.px(3)-obj.fig.Position(3)));
obj.fig.Position(2) = round( 0.5 * ( ss.px(4)-obj.fig.Position(4)));
otherwise
error( ['Unknown unit type: ', obj.defUnits,...
'. Must be either in or cm.'] );
end
% redo relative sizes
obj.setRelativeSizes;
% resize all existing subplots
if ~isempty( obj.sfPlots )
for i = 1 : length( obj.sfPlots )
obj.resizePlot( i );
end
end
end
% ================================================================
function obj = resizePlot( obj, ID )
% resize plot with handle ax according to cCol and cRow values
% for the plot with ID
% Calculate size and position
plotBottom = obj.getPlotBottom( obj.sfPlots(ID).row,...
obj.sfPlots(ID).rSpan);
plotLeft = obj.getPlotLeft( obj.sfPlots(ID).col );
plotWidth = obj.getPlotWidth( obj.sfPlots(ID).col,...
obj.sfPlots(ID).cSpan );
plotHeight = obj.getPlotHeight( obj.sfPlots(ID).row,...
obj.sfPlots(ID).rSpan );
% Update
obj.sfPlots(ID).ax( 'Position',...
[plotLeft, plotBottom, plotWidth, plotHeight] );
end
% ================================================================
function plotBottom = getPlotBottom( obj, cRow, rSpan )
% Calculates position of the bottom of a subplot at cRow row
% taking into account that the plot may span several rows
cRow = cRow + rSpan - 1;
if cRow == obj.numRows
plotBottom = obj.bp(end) + obj.bop;
else
if length( obj.bp ) == 1
sumOfBottomPaddings = obj.bp * (obj.numRows-cRow+1);
else
sumOfBottomPaddings = sum(obj.bp(cRow:end));
end
if length( obj.tp ) == 1
sumOfTopPaddings = obj.tp * (obj.numRows-cRow);
else
sumOfTopPaddings = sum(obj.tp(cRow+1:end));
end
plotBottom = (obj.numRows-cRow)*...
(obj.cellHeight * mean(obj.rowHeightWeights(cRow+1:end)))+...
+ sumOfBottomPaddings + sumOfTopPaddings + obj.bop;
end
end
% ================================================================
function plotLeft = getPlotLeft( obj, cCol )
% Calculates position of the left of a subplot at cCol column
if cCol == 1
plotLeft = obj.lp(1) + obj.lop;
else
if length( obj.lp ) == 1
sumOfLeftPaddings = obj.lp(1) * cCol;
else
sumOfLeftPaddings = sum(obj.lp(1:cCol));
end
if length( obj.rp ) == 1
sumOfRightPaddings = obj.rp(1) * (cCol-1);
else
sumOfRightPaddings = sum(obj.rp(1:cCol-1));
end
plotLeft = (cCol-1)*(obj.cellWidth * mean(obj.colWidthWeights(1:cCol-1)))+...
+ sumOfLeftPaddings + sumOfRightPaddings + obj.lop;
end
end
% ================================================================
function plotHeight = getPlotHeight( obj, cRow, rSpan )
cRow = cRow + rSpan - 1;
plotHeight = obj.cellHeight * sum(obj.rowHeightWeights(cRow-rSpan+1:cRow));
if rSpan > 1
if length( obj.tp ) > 1
plotHeight = plotHeight + sum( obj.tp( cRow-rSpan+2:cRow));
else
plotHeight = plotHeight + obj.tp(1)*( rSpan - 1 );
end
if length( obj.bp ) > 1
plotHeight = plotHeight + sum( obj.bp( cRow-rSpan+1:cRow-1));
else
plotHeight = plotHeight + obj.bp(1)*( rSpan - 1 );
end
end
end
% ================================================================
function plotWidth = getPlotWidth( obj, cCol, cSpan )
plotWidth = obj.cellWidth * sum(obj.colWidthWeights(cCol:cCol+cSpan-1));
if cSpan > 1
if length( obj.rp ) > 1
plotWidth = plotWidth + sum( obj.rp(cCol:cCol+cSpan-2));
else
plotWidth = plotWidth + obj.rp(1)*(cSpan-1);
end
if length( obj.lp ) > 1
plotWidth = plotWidth + sum( obj.lp(cCol+1:cCol+cSpan-1));
else
plotWidth = plotWidth + obj.lp(1)*(cSpan-1);
end
end
end
% ================================================================
function obj = setRelativeSizes( obj )
% Recalculates all relative sizes of paddings and unit cells
obj.checkWeightArrays;
obj.lop = obj.leftOuterPadding / obj.totalWidth;
obj.rop = obj.rightOuterPadding / obj.totalWidth;
obj.lp = obj.leftPadding ./ obj.totalWidth;
obj.rp = obj.rightPadding ./ obj.totalWidth;
obj.cellWidth = ( 1-obj.lop-obj.rop-(mean(obj.lp)+mean(obj.rp))*...
(obj.numColumns)) / sum( obj.colWidthWeights );
obj.top = obj.topOuterPadding / obj.totalHeight;
obj.bop = obj.bottomOuterPadding / obj.totalHeight;
obj.tp = obj.topPadding ./ obj.totalHeight;
obj.bp = obj.bottomPadding ./ obj.totalHeight;
obj.cellHeight = (1-obj.top-obj.bop-(mean(obj.tp)+mean(obj.bp)) *...
(obj.numRows)) / sum( obj.rowHeightWeights );
end
% ================================================================
function obj = checkWeightArrays( obj )
% Checks if rowHeightWeights and colWidthWeights are the same
% length as the number of rows and colunmns. If they are too long -
% trims, if too short then appends with ones
if length( obj.rowHeightWeights ) > obj.numRows
obj.rowHeightWeights = obj.rowHeightWeights( 1:obj.numRows);
elseif length( obj.rowHeightWeights ) < obj.numRows
for i = length( obj.rowHeightWeights )+1 : obj.numRows
obj.rowHeightWeights(i) = 1;
end
end
if length( obj.colWidthWeights ) > obj.numColumns
obj.colWidthWeights = obj.colWidthWeights( 1:obj.numColumns);
elseif length( obj.colWidthWeights ) < obj.numColumns
for i = length( obj.colWidthWeights )+1 : obj.numColumns
obj.colWidthWeights(i) = 1;
end
end
end
end
end