Skip to content

Instantly share code, notes, and snippets.

@tausen
Created December 24, 2011 13:02
Show Gist options
  • Save tausen/1517302 to your computer and use it in GitHub Desktop.
Save tausen/1517302 to your computer and use it in GitHub Desktop.
MATLAB fancy plotting
%% FANCY PLOTTING %%
% Code by Mathias Tausen, 2011 %
% Contact: mathias.tausen@gmail.com %
% Version: beta 0.5 rev1, 191211 %
%----------------------------------------------------------%
% Eases the process of plotting data with MATLAB %
% %
% How to use: %
% - Specify the data files in the files cell, syntax: %
% files = {'file1';'file2';'file3'}; %
% - Specify the columns with the data of interest in %
% the ycols array, syntax: %
% ycols = [1,2,3]; %
% - Customize the plot settings in the SETTINGS-code %
% cell. %
% - Run! %
%----------------------------------------------------------%
%----------------------------------------------------------%
% Released under the beer-ware license: %
% ---------------------------------------------------------%
% "THE BEER-WARE LICENSE" (Revision 42): %
% <mathias.tausen@gmail.com> wrote this file. As long as %
% you retain this notice you can do whatever you want with %
% this stuff. If we meet some day, and you think this %
% stuff is worth it, you can buy me a beer in return. %
% ---------------------------------------------------------%
%% SETTINGS %%
% Clear vars and close figures
clear;
close(gcf);
% Essential setup
ycols = [1,2,3]; % col to use from file as Y axis
files = ...
{'/home/tausen/Documents/scripts/effektforst-stab-diff.dat';...
'/home/tausen/Documents/scripts/effektforst-stab-diff.dat'};
% Labels, colors, legends
labels = ... % used in legend for plot=y, used for y-axes in plot=yy
{'V_s [V]','I_L [A]'};
colors = ...
{[1,0,0];[0,0,1]};
yaxislabel = 'Spaending [V]'; % not used for plot=yy
xaxislabel = 'Tid [sek]';
showlegend = false;
legendlocation = 'NorthWest';
% Axes
plotting = 'yy'; % 'y','yy'
useylim = false;
ylim = [-30 30];
usexlim = false;
xlim = [-0.001336 0.000663];
useytick = false;
ytick = linspace(-50, 150, 9);
usextick = false;
xtick = linspace(1, 15, 15);
yminortick = 'on';
xminortick = 'on';
xlabeloffset = [0 0 0];
useyticklabel = false;
unravelphase = false;
usegrid = true;
special = '';
% (only for YY plots)
ylims1 = [];
ylims2 = [];
ysteps1 = 11;
ysteps2 = 11;
% File options
delimiter = ',';
inpformat = '%f%f%f';
headerlines = 0;
uselog = true;
savefilepath = '/home/tausen/Documents/MATLAB/';
savetofile = true; % will save output.jpg and output.eps in savefilepath folder
% Calculate ytick label if chosen so (avoids x10^n)
if (useyticklabel)
a=size(ytick);
a=a(2);
for n = 1:a
yticklabel{n,1}=num2str(ytick(n));
%yticklabel{n,1}=[num2str(ytick(n)/1000),'k'];
end
end
%% IMPORTING DATA %%
% YY plots
if (strcmp(plotting,'yy'))
fullfile1 = textscan(fopen(char(files(1))), inpformat,'CommentStyle','%','Delimiter',delimiter,'Headerlines',headerlines);
fullfile2 = textscan(fopen(char(files(2))), inpformat,'CommentStyle','%','Delimiter',delimiter,'Headerlines',headerlines);
Freq = fullfile1{1,ycols(1)};
data1 = fullfile1{1,ycols(2)};
data2 = fullfile2{1,ycols(3)};
% Make phase prettier if chosen so (assuming data2 contains the phase)
if (unravelphase)
x = 0;
for x = 2:(length(data2))
if (data2(x)-data2(x-1) > 10) % assume that >10deg jump is an error
data2(x) = -180-(180-data2(x));
end
end
end
end
% Y plots
if (strcmp(plotting,'y'))
% Loop through each file and save specified cols in dataX and dataY
for i = 1:length(ycols)
fullfile = textscan(fopen(char(files(i))), inpformat,'CommentStyle','%','Delimiter',delimiter,'Headerlines',headerlines);
dataY{i} = fullfile(ycols(i));
dataX{i} = fullfile(1);
end
end
%% PLOTTING FIGURE %%
% Prep the figure
fig1 = figure('XVisual',...
'0x93 (TrueColor, depth 24, RGB mask 0x3ff0000 0xff00 0x00ff)',...
'units','pixels','Position',[100 100 800 450]);
fig1axes = axes('Parent',fig1,'XMinorTick',xminortick);
if (uselog)
set(fig1axes,'XScale','log');
end
% Plot data in figure
if (strcmp(plotting,'yy'))
[AX H1 H2] = plotyy(Freq,data1,Freq,data2,'Parent',fig1axes);
% Set labels and properties
set(get(AX(1),'Ylabel'),'String',labels(1),'FontSize',14);
set(get(AX(2),'Ylabel'),'String',labels(2),'FontSize',14);
if (usexlim)
set(AX(1),'Xlim',xlim);
set(AX(2),'Xlim',xlim);
end
if (uselog)
set(AX(1),'Xscale','log');
set(AX(2),'Xscale','log');
end
set(AX(1),'YMinorTick','on');
set(AX(2),'YMinorTick','off');
% Set colors
set(AX(1),'Ycolor',colors{1,1});
set(H1,'Color',colors{1,1});
set(AX(2),'Ycolor',colors{2,1});
set(H2,'Color',colors{2,1});
% Set limits and ticks
if (useylim)
set(AX(1),'Ylim',ylims1);
set(AX(2),'Ylim',ylims2);
ytick1 = linspace(ylims1(1),ylims1(2),ysteps1);
set(AX(1),'YTick',ytick1);
ytick2 = linspace(ylims2(1),ylims2(2),ysteps2);
set(AX(2),'YTick',ytick2);
a=size(ytick1);
a=a(2);
for n = 1:a
yticklabel1{n,1}=strcat(num2str(ytick1(n)));
end
set(AX(1),'YTickLabel',yticklabel1);
a=size(ytick2);
a=a(2);
for n = 1:a
yticklabel2{n,1}=num2str(ytick2(n));
end
set(AX(2),'YTickLabel',yticklabel2);
end
end
% Set labels and properties
if (strcmp(plotting,'y'))
hold on;
for n = 1:length(ycols)
if (uselog)
d = semilogx(dataX{1,n}{1,1}, dataY{1,n}{1,1},'Parent',fig1axes);
else
d = plot(dataX{1,n}{1,1}, dataY{1,n}{1,1},'Parent',fig1axes);
end
set(d, 'Color',colors{n,1});
set(d, 'DisplayName', char(labels(n)));
end
if (showlegend)
legendHandle = legend(fig1axes,'show');
set(legendHandle,'Location',legendlocation);
end
ylabel(yaxislabel,'FontSize',14);
set(fig1axes,'YMinorTick',yminortick);
if (useytick)
set(fig1axes,'YTick',ytick);
end
if (useylim)
set(fig1axes,'Ylim',ylim);
end
if (usextick)
set(fig1axes,'XTick',xtick);
end
end
xlabel(xaxislabel,'FontSize',14);
% fix x label position
xlabh = get(gca,'XLabel');
set(xlabh,'Position',get(xlabh,'Position') + xlabeloffset)
if (usexlim)
set(fig1axes,'Xlim',xlim);
end
if (useyticklabel)
set(fig1axes,'YTickLabel',yticklabel);
end
if (usegrid)
grid;
end
% Export the figure
if (savetofile)
set(gcf, 'PaperUnits', 'points');
set(gcf, 'PaperSize', [1024 400]);
myfiguresize = [10, 10, 850, 250];
set(gcf, 'PaperPosition', myfiguresize);
saveas(gcf, strcat(savefilepath, 'output.eps'), 'psc2');
saveas(gcf, strcat(savefilepath, 'output.jpg'), 'jpg');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment