Commit a1956544 by Christian Hoffmann

Figure template for Matlab

parent c7f3c5c2
clear
close all
clc
%% LaTeX editor settings
set(groot,'defaultTextInterpreter','tex'); % how input is interpreted
set(groot, 'defaultAxesTickLabelInterpreter','tex');
set(groot, 'defaultLegendInterpreter','tex');
%% Create class objects
Figure = figureTemplateClass;
Figure2 = figureTemplateClass;
%% Configure object 1
Figure.dataFileName = 'TemplateData.dat'; % file name of the loaded data
Figure.savePath = 'TemplatePathExample'; % path in which the figure shall be saved
Figure.figureName = 'Template'; % what is the figure's name
Figure.fontName = 'arial';
Figure.axisSpecs = [-2 2 0.5 -4 4 1.0]; % axis limits for both axes [xstart xend xstep ystart yend ystep]
Figure.xTickFormat = '%.1f'; % how many decimal numbers for x axis
Figure.yTickFormat = '%.2f'; % how many decimal numbers for y axis
Figure.xDescription = 'Variable';
Figure.xVariable = 'x';
Figure.xUnit = '-';
Figure.yDescription = 'Variable';
Figure.yVariable = 'y';
Figure.yUnit = '-';
Figure.savePNG = false; % define the file formats that shall be generated
Figure.savePDF = true;
Figure.saveSVG = true;
if ~exist(Figure.savePath,'dir') % check whether path exists, if not create it!
mkdir(savePath)
end
%% Configure object 2
Figure2.dataFileName = 'TemplateData.dat'; % file name of the loaded data
Figure2.savePath = 'TemplatePathExample'; % path in which the figure shall be saved
Figure2.figureName = 'Template2'; % what is the figure's name
Figure2.fontName = 'times';
Figure2.axisSpecs = [1E-4 1E-3 1E-4 1E-6 1E6 1E5]; % axis limits for both axes [xstart xend xstep ystart yend ystep]
Figure2.xTickFormat = '%.1f'; % how many decimal numbers for x axis
Figure2.yTickFormat = '%.1f'; % how many decimal numbers for y axis
Figure2.xDescription = 'Molar volume';
Figure2.xVariable = 'v';
Figure2.xUnit = 'm^3 mol^{-1}';
Figure2.yDescription = 'Pressure';
Figure2.yVariable = 'P';
Figure2.yUnit = 'Pa';
Figure2.savePNG = false; % define the file formats that shall be generated
Figure2.savePDF = true;
Figure2.saveSVG = true;
if ~exist(Figure2.savePath,'dir') % check whether path exists, if not create it!
mkdir(savePath)
end
%% Data generated in Matlab for object 1
maximumPower=4;
x=linspace(-2,2,100);
y=zeros(maximumPower+1,length(x));
for i=0:1:maximumPower
y(i+1,:)=x.^i;
end
%% Data read from .dat file for object 2
fileData = readtable(Figure.dataFileName,'format','%f%f');
xValues = table2array(fileData(:,1));
yValues = table2array(fileData(:,2));
%% Generate plot for object 1
Figure.createPlot() % creates a plot based on the above specifications
set(groot, 'defaultAxesFontName', Figure.fontName); % set font name for all figures
L = cell(1,size(y,1));
for yIndex=1:size(y,1)
plot(x,y(yIndex,:),'lineWidth',Figure.lineWidth,'lineStyle',Figure.lineStyle{yIndex},'Color',Figure.color(yIndex,:),'marker',Figure.markerStyle{yIndex});
hold on
L{yIndex} = sprintf('y_%i',yIndex);
end
legend(L,'location','best')
Figure.setXLabel;
Figure.setYLabel;
Figure.savePlot;
%% Generate plot for object 2
Figure2.createPlot()
set(groot, 'defaultAxesFontName', Figure2.fontName); % set font name for all figures
plot(xValues,yValues,'lineWidth',Figure2.lineWidth,'lineStyle',Figure2.lineStyle{1},'Color',Figure2.color(1,:),'marker',Figure2.markerStyle{1});
Figure2.setXLabel;
Figure2.setYLabel;
Figure2.xAxisLogScale() % for log x axis
% Figure2.yAxisLogScale() % for log y axis
Figure2.savePlot;
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
classdef figureTemplateClass < handle
properties % default properties are given
figureName = 'figureTemplate';
fontName = 'arial'; % font name
dataFileName = ''; % file name of the data
axisSpecs = [0 1 0.1 0 1 0.1]; % axis limits for both axes [xstart xend xstep ystart yend ystep]
xTickFormat = '%.1f'; % how many decimal numbers for x axis
yTickFormat = '%.1f'; % how many decimal numbers for y axis
xDescription = 'Variable';
xVariable = 'x';
xUnit = '-';
yDescription = 'Variable';
yVariable = 'y';
yUnit = '-';
savePath = 'Figures';
savePNG = false;
savePDF = true;
saveSVG = false;
end
properties (SetAccess = private, GetAccess=public)
fontSize = 18 ; % font size
lineWidth = 3; % line width
lineStyle = {'-','--','-.',':','-'}; % possible line styles
markerStyle = {'s','p','v','^','x'}; % possible markers
color = [ % possible colors (corporate design TUB)
197 14 31
0 0 0
113 113 113
243 148 0
0 121 154
0 123 106
]/255;
paperSize = [20 15]; % how large is the figure (in cm)
end
methods
function createPlot(self)
% Figure Name
f = figure;
set(f,'NumberTitle','off','name',self.figureName);
% Set Font, Options, and Image Size
set(gca, 'FontName', self.fontName)
set(gcf, 'Units', 'Centimeters', 'Position', [5 5 self.paperSize], 'PaperUnits', 'Centimeters', 'PaperSize', self.paperSize)
set(gca,'FontSize',self.fontSize, 'FontUnits','points');
%% Plot Design
ax=gca;
set(gca,'TickDir','out'); % The only other option is 'in'
box on
grid on
hold on
ax.XColor = 'black';
ax.YColor = 'black';
xl = self.axisSpecs(1);
xh = self.axisSpecs(2);
xi = self.axisSpecs(3);
yl = self.axisSpecs(4);
yh = self.axisSpecs(5);
yi = self.axisSpecs(6);
xlim([xl xh])
ylim([yl yh])
xtickformat(self.xTickFormat)
ytickformat(self.xTickFormat)
ax.XAxis.TickValues = xl:xi:xh;
ax.YAxis.TickValues = yl:yi:yh;
end
function setXLabel(self)
xlabel(append(self.xDescription, ' {\it', self.xVariable, '} in ', self.xUnit))
end
function setYLabel(self)
ylabel(append(self.yDescription, ' {\it', self.yVariable, '} in ', self.yUnit))
end
function xAxisLogScale(self)
set(gca,'xscale','log')
xtick=10.^(log10(self.axisSpecs(1)):log10(self.axisSpecs(2)));
xticklab = cellstr(num2str(round(log10(xtick(:))), '10^{%d}'));
set(gca,'XTick',xtick,'XTickLabel',xticklab,'TickLabelInterpreter','tex')
end
function yAxisLogScale(self)
set(gca,'yscale','log')
ytick=10.^(log10(self.axisSpecs(4)):log10(self.axisSpecs(5)));
yticklab = cellstr(num2str(round(log10(ytick(:))), '10^{%d}'));
set(gca,'YTick',ytick,'YTickLabel',yticklab,'TickLabelInterpreter','tex')
end
function savePlot(self)
if self.savePNG
print(sprintf('%s/%s.png',self.savePath,self.figureName),'-dpng','-r600')
end
if self.savePDF
print(sprintf('%s/%s.pdf',self.savePath,self.figureName),'-dpdf')
end
if self.saveSVG
print(sprintf('%s/%s.svg',self.savePath,self.figureName),'-dsvg')
end
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment