% This file contains one first section in which the use of figure, axis,
% and objects handles is demonstrated
% The second section creates a multi-panel graph that contains an image, a line
% plot, a bar chart, a scatter plot, and a pie chart
% The third section exports this figure into different file formats,
% with different resolutions and color schemes
%
% Written by Christian Ruff, March 2007

clear all; close all;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Section 1: Illustrate handles for  figures, axes, and objects

% plot some example data and illustrate handles
data = [sin(0:0.1:2*pi); ...
        cos(0:0.1:2*pi)]';

plot(data)

% two ways to get the handle of the current figure
fig_handle  = get(0,'CurrentFigure')
fig_handle  = gcf % gcf means "get current figure"

% three ways to get the handle of the current axes
ax_handle   = get(gcf,'CurrentAxes')
ax_handle   = get(gca)

% we can specify the figure with its handle
ax_handle   = get(fig_handle,'CurrentAxes')

% get handles for the axes that are in the current figure
% 'Children' refers to all elements of the lower hierarchy (here axes)
ax_handles = get(gcf,'Children')

% In the context of axes, 'Children' refers to all objects
obj_handles = get(gca,'Children')

% we can also find the handle for one specific object
sine        = findobj('color','blue')

% now look at all properties of the first object
obj_prop   = get(obj_handles(1))

% we can also only access one property
sine_thick = get(sine,'linewidth')

% and we can do the same for axes and figures
ax_prop   = get(ax_handle)
fig_prop  = get(fig_handle,'color')

% we can change a property by "set"
set(sine,'LineWidth',3)

% we can list as many property-pairs as we want, in one go
set(sine,'LineWidth',3,'Linestyle','-.','color','g')
set(obj_handles(1),'Linestyle','o','color','r')
set(ax_handle,'color',[0 0 0])
set(fig_handle,'color',[1 1 1])


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Section 2: We create a multi-panel figure with different axes
% These contain an image, a line plot, a bar chart, a scatter plot,
% and a pie chart

% close all existing figures
clear all; close all;

% create figure 1
FIG(1).handle = figure(1);

% set it to a given size and color
set(FIG(1).handle,'Position',[200 50 1000 600],'Color',[1 1 1]);


% create the first axis for the figure title
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FIG(1).axis(1).handle = axes('position',[0.1 0.9 0.8 0.1],'color',[1 1 1])
FIG(1).axis(1).title = text(0.3,0.5,'Example figure')
set(FIG(1).axis(1).title,'Fontname','Helvetica','Fontsize',20,'color',[0 0 0])

% make axis invisible
axis off


% create second axis and show image in it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FIG(1).axis(2).handle = axes('position',[0.01 0.55 0.3 0.3])
load mandrill % loads a default matlab image
imagesc(X); % displays the matrix graphically

% title is a text command that puts the text already above the axis
FIG(1).axis(2).title = title('A monkey','fontsize',15)

% the axis command allows you to specify the shape of the axis
axis image  % makes axis fit the image
axis off    % makes axis disappear

% we often have to specify the colormap to tell MATLAB
% what the numbers in the image matrix mean
% the command "colormap" by itself returns the current colormap
size(X)
current_map = colormap

% there are several default colormaps stored in matlab,
% see "help graph3d" and the demo "imageext"
colormap gray

% often images come with their colormap; this one does (the array "map" is
% stored with the file "mandrill")
colormap(map)

% you can also load images for display by the command imread
cla % clears the content of the current axes
X = imread('man.jpg');
imagesc(X)

axis square % forces the axis to be square, irrespective of image dimensions
axis image off
FIG(1).axis(2).title = title('A man','fontsize',15)


% now we create an axis for a plot of the sine-cosine data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FIG(1).axis(3).handle = axes('position',[0.4 0.55 0.5 0.3])

data = [sin(0:0.1:2*pi); ...
        cos(0:0.1:2*pi)]';

FIG(1).axis(3).obj   = plot(data)
FIG(1).axis(3).title = title('Trigonometry','fontsize',15)

% we want the box with a thick line and without a grid
grid off; % versus grid on
box off; % versus box on
set(FIG(1).axis(3).handle,'linewidth',1.5)

% we change the limits of the axes
axis tight % sets axes to the limits of the data
axis([0 64 -1.5 1.5]) % sets axes to user specified limits

% and label them differently
set(FIG(1).axis(3).handle, ...
    'XTick',[32 64], ...
    'XTickLabel',{'pi','2*pi'}, ...
    'FontSize',14);

% we can fully define many different properties of the plots
set(FIG(1).axis(3).obj(1), ...
    'LineWidth',1.5, ...
    'Marker','s', ...
    'MarkerSize',4);

set(FIG(1).axis(3).obj(2), ...
    'LineWidth',1.5, ...
    'LineStyle','>', ...
    'MarkerSize',3);

% we can create legends
FIG(1).axis(3).legend = legend(FIG(1).axis(3).obj,{'sine','cosine'},3);
set(FIG(1).axis(3).legend,'Fontsize',12)


% now let's create an axis for a bar chart
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FIG(1).axis(4).handle = axes('position',[0.08 0.1 0.25 0.3])
box on;

% some fake reaction time data
data = repmat([500 200 750 400],12,1)+(rand(12,4)-0.5)*300;

% we could plot all column means as one bar-group
FIG(1).axis(4).obj(1) = bar(mean(data))

% or we can create a grouped bar graph
cla;
FIG(1).axis(4).obj = ...
    bar([mean(data(:,[1 2]));mean(data(:,[3 4]))],'grouped')

% change color of bars
set(FIG(1).axis(4).obj(1), ...
    'FaceColor',[0.5 0 0])

set(FIG(1).axis(4).obj(2), ...
    'FaceColor',[0 0 0.5])

% and label the axes
title('Reaction times','fontsize',14)
xlabel('Condition','fontsize',12)
ylabel('RT [ms]','fontsize',12)

% change numbers on x- and y-axis
ylim([0 1000])
set(FIG(1).axis(4).handle, ...
    'XTickLabel',{'one','two'}, ...
    'YTick',[0 500 1000], ...
    'YTickLabel',{'0','500','1000'}, ...
    'FontSize',11);

% error bars will need to be created with the separate function errorbar
% hold on is very important; it allows us to keep plotting in the axis without
% replacing it
hold on;
FIG(1).axis(4).errorbar = ...
    errorbar([0.85 1.15 1.85 2.15],mean(data), ...
    [0 0 0 0],std(data));

% set the whiskers to black and the connecting lines to invisible
set(FIG(1).axis(4).errorbar,'color',[0 0 0],'linestyle','none')



% now a scatterplot
%%%%%%%%%%%%%%%%%%%
FIG(1).axis(5).handle = axes('position',[0.43 0.1 0.25 0.3],'linewidth',1.5)
box on; hold on;

% random data with 4 columns
data = rand(50,4)*100;

% first group shows no association
scatter(data(:,1),data(:,2),20,'b')
% second shows strong association
scatter(sort(data(:,3)),sort(data(:,4)),20,'r')

% set axis limits and add title
axis([-50 150 -50 150])
title('A scatterplot','fontsize',14)

% label axes
xlabel('Variable A','fontsize',12)
ylabel('Variable B','fontsize',12)
set(FIG(1).axis(5).handle, ...
    'FontSize',11);

% legend for both figures
FIG(1).axis(4).legend = legend(FIG(1).axis(4).obj,{'patients','controls'},1);

% move legend outside of plot; we'll need it for both plots
set(FIG(1).axis(4).legend,'Fontsize',11,'position',[0.32 0.35 0.12 0.08])


% and now, finally, a 3-dimensional pie chart
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FIG(1).axis(6).handle = axes('position',[0.75 0.01 0.2 0.3],'linewidth',1.5)
axis off;

labels = {'A','','B',''};
FIG(1).axis(6).obj = pie3([0.2 0.3 0.4 0.1],[0 0 1 0],labels)

% nicer color, please
colormap hot

% find the objects that are the labels, and change their size and color
for i = 1:length(labels)
    t = findobj('String',labels{i})
    set(t,'FontSize',18,'color',[0 0 0.7])
end

% axes remain visible if you create others on top of them
FIG(1).axis(7).handle = axes('position',[0.75 0.1 0.2 0.3],'linewidth',1.5)
axis off;
labels = {'A','','B',''};
FIG(1).axis(7).obj = pie3([0.2 0.3 0.4 0.1],[0 0 1 0],labels)
for i = 1:length(labels)
    t = findobj('String',labels{i})
    set(t,'FontSize',18,'color',[0 0 0.7])
end

% add title
title('A bunch of pies','fontsize',14)



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Section 3: We can export the figure into different formats and resolutions

print -f1 -dtiff -r70 figure1.tiff % low-resoltuion tiff
print -f1 -djpeg -r600 -cmyk figure1.jpg % high-resolution jpeg
print -f1 -depsc -tiff -r600 figure1.eps % high-resolution eps with tiff preview

% however, it is important that we set some figure properties first,
% to specify that we want the figure as it is on the screen
% Otherwise the figure might look distorted when exported,
% or will be printed on different paper
set(FIG(1).handle, ...
    'PaperPositionMode','auto', ... % makes size ratios stay as on the screen
    'PaperOrientation','landscape', ... % makes sure we print on portrait paper
    'InvertHardCopy','off');    % makes sure the background colors stay the same

print -f1 -dtiff -r70 figure1.tiff % low-resolution tiff
print -f1 -djpeg -r600 -cmyk figure1.jpg % high-resolution jpeg with cmyk color
print -f1 -depsc -tiff -r600 figure1.eps % high-resolution eps with tiff preview