-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaceScalebar.m
executable file
·51 lines (39 loc) · 1.8 KB
/
PlaceScalebar.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
function PlaceScalebar(pixelSize,magnification)
%% Grab width and height from the plot
imageWidth = abs(diff(get(gca,'XLim')));
imageHeight = abs(diff(get(gca,'YLim')));
%% Determine the size of the scalebar
% We want the width of the scalebar to be approximately 25% of the width
% of the image. Furthermore do we want the scalebar to represent an
% integer number of units (1 um, 2 um, 10 um, 100 nm,...). Therefore we
% first see what the what size we represent if the scalebar is 25% of the
% width of the image, and then find the closest integer number.
startSize = .25*imageWidth * (pixelSize/magnification);
% Find the power of ten we need, going from 1 nm to 100 mm
for j=-6:3
if floor(startSize *10^(j)) >= 1
break; %will exit the loop
end
end
% Set the unit
if (j > 0) && (j <= 3)
sizeUnit = 'nm';
powerTen = 3;
elseif (j > -3) && (j <= 0)
sizeUnit = '\mum';
powerTen = 0;
elseif (j > -6) && (j <= -3)
sizeUnit = 'mm';
powerTen = -3;
end
% Set the size
sizeBar = round(startSize *10^(j)) .*10^(-j) .*10^(powerTen);
scaleWidth = sizeBar .*10^(-powerTen) * (magnification/pixelSize);
scaleHeight = scaleWidth / 6;
scaleXPos = round( imageWidth - (.075*imageHeight) - scaleWidth );
scaleYPos = round( imageHeight - (.075*imageHeight) - scaleHeight );
textXPos = scaleXPos + 0.5*scaleWidth;
textYPos = scaleYPos - 1*scaleHeight;
rectangle('Position', [scaleXPos, scaleYPos, scaleWidth, scaleHeight],'FaceColor',[1 1 0.99]);
text(textXPos, textYPos,[num2str(sizeBar) ' ' sizeUnit], 'Fontsize', 30, 'HorizontalAlignment', 'Center','Color',[1 1 0.99]);
end