% clear command window, figures and all variables
clc; clf; clear all; 

%% Common variables

FontSize = 12;
FontName = 'MyriadPro-Regular'; % or choose any other font

doExportPlot = true;
languageEnglish = false;

%% Definition of physical constants

a = 0.25; % focal radius (radial)
b = 1; % focal length (axial)
k = 5;

% calculation of axis
xMax = 15;
xPoints = 1000;
xAxis = linspace(-xMax, xMax, xPoints);

yMax = 12;
yPoints = 1000;
yAxis = linspace(-yMax, yMax, yPoints);


%% functions, here inline since they are rather simple
Gaussian = @(x,y) ( exp(-(x.^2 + y.^2) ) );
Sombrero = @(x,y) ( sin(sqrt(x.^2+y.^2)) ./ sqrt(x.^2+y.^2) );
Ripple = @(x,y) ( (cos(sqrt(x.*x+y.*y) + cos(sqrt(((x+.913*2*pi).*(x+.913*2*pi))+y.*y)) + cos(sqrt(((x-.913*2*pi).*(x-.913*2*pi))+(y.*y))))*4) );


%% Calculation
% calculate in single line (fastest method)
xMat = (xAxis' * ones([1 yPoints]))';
yMat = yAxis' * ones([1 xPoints]);
tic;
Data2D = Ripple(xMat, yMat);
display(['calculated ' num2str(xPoints*yPoints) ' datapoints in ' num2str(toc) ' seconds']);

Data2D = Data2D - min(min(Data2D)); % normalize to 0-1
Data2D = Data2D / max(max(Data2D)); 

%% start plot

% figure dimensions in cm. I choose 1.5 or 2 times 
% the target size typically. If figure is display in
% document much smaller increase the fontsize.

figure_width = 14;  
figure_height = 10;

% --- setup plot windows
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
hfig = figure(1); clf;
    set(hfig,'Visible', figuresVisible)

    set(hfig, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])   
    set(hfig, 'PaperPositionMode', 'auto');    
    set(hfig, 'Renderer','Zbuffer'); 
    set(hfig, 'Color', [1 1 1]); % Sets figure background
    set(gca, 'Color', [1 1 1]); % Sets axes background

% --- dimensions and position of plot 
hsp = subplot(1,1,1, 'Parent', hfig);
set(hsp,'Position',[0.15 0.17 0.75 0.80]);
    
%% plot
colorDepth = 1000;
colormap(jet(colorDepth));
pcolor(xAxis, yAxis, Data2D); 

%%  setup axis plot properties
% shading interp; % interpolate pixels
shading flat; % do not interpolate pixels
axis on;      % display axis
axis tight;   % no white borders
axis image;   % real x,y scaling


set(gca, ...
    'CLim' , [0 1], ...
    'Box'         , 'on'      , ...
    'TickDir'     , 'in'      , ...
    'TickLength'  , [.015 .015] , ...
    'XMinorTick'  , 'off'      , ...
    'YMinorTick'  , 'off'     , ...
    'XGrid'       , 'off'     , ...
    'YGrid'       , 'off'     , ...
    'XColor'      , [.0 .0 .0], ...
    'YColor'      , [.0 .0 .0], ...
    'LineWidth'   , 0.6        );

%% axis scales

set(gca,'XTick',-15:5:15)
set(gca,'YTick',-10:5:10)

%% label texts
TITLE = '';
if (languageEnglish)
    xLabelText = 'x / µm';  % greek letters in LaTeX Syntax
    yLabelText = 'y / µm';
    zLabelText = 'normalized intensity';
else
    xLabelText = 'x / µm';
    yLabelText = 'y / µm';
    zLabelText = 'normierte Intensität';
end

% save handles to set up label properties
hTitle = title(TITLE);
hXLabel = xlabel(xLabelText);
hYLabel = ylabel(yLabelText);


%% colorbar
caxis([0 1]);
hcb = colorbar('location','EastOutside');
hcLabel = ylabel(hcb,zLabelText);
set(hcb,'YTickLabel',0:0.2:1, 'Ytick', 0:0.2:1)
set(hcb,'YTickMode','manual')
set(hcb, ...
  'Box'         , 'on'     , ...
  'TickDir'     , 'in'     , ...
  'TickLength'  , [.015 .015] , ...
  'LineWidth'   , 0.6);


%% set properties for all handles
set([gca, hcb, hTitle, hXLabel, hYLabel, hcLabel], ...
    'FontSize'   , FontSize    , ...
    'FontName'   , FontName);


%% last 'bug' fixes

% change decimal point to ',' for german text
if (~languageEnglish)
     fixLabels(gca);
     fixLabels(hcb);
end
% bring axis on top again (fix matlab bug)
set(gca,'Layer', 'top');

%% export
drawnow

SaveDir = '';
SaveName = 'pcolor-example';

if (doExportPlot)
    IMAGENAME = [SaveDir SaveName]; 
    print(hfig, ['-r' num2str(400)], [IMAGENAME '.png' ], ['-d' 'png']);    
    crop([IMAGENAME '.png']);
    display('finished plot export')
end

% close(hfig);

    