Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • skyward/matlab-dependencies/common
1 result
Select Git revision
Show changes
Commits on Source (8)
function exportFigureGUI
% exportFigureGUI - GUI for exportStandardizedFigure function
%% Positioning GUI at screen center
% Get the screen size
screenSize = get(0, 'ScreenSize'); % [left, bottom, width, height]
% Set the uifigure size
figWidth = 1400;
figHeight = 700;
% Calculate position to center the figure
figX = (screenSize(3) - figWidth) / 2;
figY = (screenSize(4) - figHeight) / 2;
%% Create main figure window
fig = uifigure('Name', 'Export Standardized Figure', 'Position', [figX, figY, figWidth, figHeight]);
% Add title
titleLabel = uilabel(fig);
titleLabel.Text = 'Figure Export Settings';
titleLabel.FontSize = 18;
titleLabel.FontWeight = 'bold';
titleLabel.HorizontalAlignment = 'center';
titleLabel.Position = [0, fig.Position(4)-50, fig.Position(3), 30];
%% Input fields
% Figure Name
uilabel(fig, 'Text', 'Figure Name:', 'Position', [50 550 100 30]);
figNameField = uitextarea(fig, 'Position', [150 550 200 30]);
% WHratio Input
uilabel(fig, 'Text', 'Width/Height Ratio:', 'Position', [50 170 150 30]);
WHratioField = uieditfield(fig, 'numeric', 'Value', 0, 'Position', [180 170 50 30]);
% Forced Markers Input
uilabel(fig, 'Text', 'Forced Markers:', 'Position', [270 170 150 30]);
forcedMarkersField = uieditfield(fig, 'numeric', 'Value', 0, 'Position', [390 170 50 30]);
% Percentage of Text Width
uilabel(fig, 'Text', 'Percentage of Text Width:', 'Position', [500 170 200 30]);
percTextField = uieditfield(fig, 'numeric', 'Position', [670 170 50 30]);
%% Checkboxes
%%% Create a panel to group flags
flagsPanel = uipanel(fig, ...
'Title', 'Flags', ...
'FontSize', 12, ...
'Position', [40 400 720 70]); % Position: [x, y, width, height]
% Checkboxes for optional parameters
addMarkersBox = uicheckbox(flagsPanel, 'Text', 'Add Markers', 'Value', true, ...
'Position', [10 10 150 20]);
changeColorsBox = uicheckbox(flagsPanel, 'Text', 'Change Colors', 'Value', true, ...
'Position', [120 10 150 20]);
changeLineStyleBox = uicheckbox(flagsPanel, 'Text', 'Change Line Style', 'Value', false, ...
'Position', [240 10 150 20]);
gridBox = uicheckbox(flagsPanel, 'Text', 'Show Grid', 'Value', true, ...
'Position', [370 10 150 20]);
satelliteMapColorsBox = uicheckbox(flagsPanel, 'Text', 'Satellite Map Colors', 'Value', false, ...
'Position', [480 10 150 20]);
%%% Create a panel to group export settings
exportPanel = uipanel(fig, ...
'Title', 'Export setting', ...
'FontSize', 12, ...
'Position', [40 310 720 70]); % Position: [x, y, width, height]
% Checkboxes for optional parameters
exportPDFBox = uicheckbox(exportPanel, 'Text', 'Export PDF', 'Value', true, ...
'Position', [10 10 150 20]);
exportFIGBox = uicheckbox(exportPanel, 'Text', 'Export FIG', 'Value', false, ...
'Position', [120 10 150 20]);
overwriteFigureBox = uicheckbox(exportPanel, 'Text', 'Overwrite', 'Value', false, ...
'Position', [240 10 150 20]);
%% Dropdown menus
%%% Create a panel to group dropdown menus
legendPanel = uipanel(fig, ...
'Title', 'Legend', ...
'FontSize', 12, ...
'Position', [40 220 720 70]); % Position: [x, y, width, height]
% Dropdowns for Legend Location and Orientation
uilabel(legendPanel, 'Text', 'Location:', 'Position', [10 10 60 20]);
legendPositions = {'north', 'south', 'east', 'west', ...
'northeast', 'northwest', 'southeast', 'southwest', ...
'northoutside', 'southoutside', 'eastoutside', 'westoutside', ...
'northeastoutside', 'northwestoutside', 'southeastoutside', 'southwestoutside', ...
'best', 'bestoutside'};
legendLocationDropDown = uidropdown(legendPanel, 'Items', legendPositions, ...
'Value', 'southoutside', ...
'Position', [80 10 150 22]);
uilabel(legendPanel, 'Text', 'Orientation:', 'Position', [250 10 70 20]);
legendOrientations = {'horizontal', 'vertical'};
legendOrientationDropDown = uidropdown(legendPanel, 'Items', legendOrientations, ...
'Value', 'horizontal', ...
'Position', [330 10 150 22]);
%% Preview content
uiPanel = uipanel(fig, 'Title', 'Export preview', 'Position', [810, 100, 550, 500]);
% Find all open figures (excluding this GUI)
figHandles = findFigures(fig);
figNames = getFigureNames(figHandles);
% Dropdown menu to select figure
uilabel(fig, 'Text', 'Select Figure:', 'Position', [50, 500, 100, 30]);
figSelectionDropDown = uidropdown(fig, ...
'Items', figNames, ...
'Position', [150, 500, 200, 30], ...
'ValueChangedFcn', @(src, event) updatePreview());
% Automatically update preview if only one figure exists
if isscalar(figHandles)
updatePreview();
end
%% Buttons
% Button to update the dropdown menu
updateButton = uibutton(fig, 'Text', 'Update Figures', ...
'Position', [370, 500, 120, 30], ...
'ButtonPushedFcn', @(src, event) updateDropdown());
% Submit Button
submitButton = uibutton(fig, 'Text', 'Export Figure', ...
'Position', [350 100 100 30], ...
'ButtonPushedFcn', @(src, event) exportFigureCallback());
% preview image to export button
previewButton = uibutton(fig, 'Text', 'Update preview', ...
'Position', [500, 500, 120, 30], ...
'ButtonPushedFcn', @(src, event) previewFigureCallback());
%% Functions
% Function to update dropdown menu with new figures
function updateDropdown()
figHandles = findFigures(fig);
figNames = getFigureNames(figHandles);
figSelectionDropDown.Items = figNames;
% Automatically update preview if only one valid figure exists
if isscalar(figHandles)
updatePreview();
end
end
% Function to update preview
function updatePreview()
% Delete any existing axes
delete(findall(uiPanel, 'Type', 'figure'));
figHandles = findFigures(fig);
% Get selected figure from dropdown
selectedIdx = figSelectionDropDown.Value;
selectedFig = [];
% Find the selected figure
for i = 1:length(figHandles)
if strcmp(figSelectionDropDown.Items{i}, selectedIdx)
selectedFig = figHandles(i);
break;
end
end
if isempty(selectedFig) && isscalar(figHandles), selectedFig = figHandles(1); end
if isempty(selectedFig) || ~isvalid(selectedFig), return; end
% Retrieve axes from selected figure
obj = findall(selectedFig, 'Type', 'figure');
% Delete any existing axes
delete(findall(uiPanel, 'Type', 'axes'));
% Copy the axes into the UI Panel
check = size(obj.Children);
if check(1) == 1
newAx = copyobj(obj.Children, uiPanel);
hold(newAx, 'on');
end
if check(1) == 2 % geoplots or 3d plots or tabfigures
if isa(obj.Children(1), 'matlab.ui.container.TabGroup')
numTabs = numel(obj.Children(1).Children); % Get number of tabs
originalFigNum = obj.Number; % Get the original figure number
for i = 1:numTabs
newFigName = sprintf('Figure %d - Tab %d', originalFigNum, i);
newFig = figure('Name', newFigName, 'NumberTitle', 'off');
newAx = copyobj(obj.Children(1).Children(i).Children, newFig);
set(newAx, 'Position', get(gca, 'Position'));
hold(newAx, 'on');
end
else
newAx = copyobj(obj.Children(2), uiPanel);
hold(newAx, 'on');
end
end
if check(1) > 2
if isa(obj.Children(1), 'matlab.graphics.illustration.Legend')
newAx = copyobj(obj.Children(2:end), uiPanel);
hold(newAx(1:check(1)-1), 'on');
else
newAx = copyobj(obj.Children, uiPanel);
hold(newAx, 'on');
end
end
%set(newAx, 'Position', [0.1, 0.1, 0.8, 0.8]); % Adjust position in panel
%hold(newAx, 'on');
% Apply user settings
linesInPlot = findall(newAx, 'Type', 'line');
if addMarkersBox.Value
for line = linesInPlot'
set(line, 'Marker', 'o', 'MarkerSize', 4);
end
end
if changeColorsBox.Value
set(newAx, 'ColorOrder', lines(7));
end
if gridBox.Value
grid(newAx, 'on');
else
grid(newAx, 'off');
end
if changeLineStyleBox.Value
for line = linesInPlot'
set(line, 'LineStyle', '--');
end
end
if satelliteMapColorsBox.Value
colormap(newAx, 'parula');
end
if WHratioField.Value > 0
daspect(newAx, [1 WHratioField.Value 1]);
end
% Ensure preview updates on checkbox change
addMarkersBox.ValueChangedFcn = @(src, event) updatePreview();
changeColorsBox.ValueChangedFcn = @(src, event) updatePreview();
gridBox.ValueChangedFcn = @(src, event) updatePreview();
WHratioField.ValueChangedFcn = @(src, event) updatePreview();
changeLineStyleBox.ValueChangedFcn = @(src, event) updatePreview();
satelliteMapColorsBox.ValueChangedFcn = @(src, event) updatePreview();
legendLocationDropDown.ValueChangedFcn = @(src, event) updatePreview();
legendOrientationDropDown.ValueChangedFcn = @(src, event) updatePreview();
hold(newAx, 'off');
end
% Callback Function for Button
function exportFigureCallback()
% Get values from the GUI components
figName = figNameField.Value;
percTextwidth = percTextField.Value;
addMarkers = addMarkersBox.Value;
forcedMarkers = forcedMarkersField.Value;
changeColors = changeColorsBox.Value;
changeLineStyle = changeLineStyleBox.Value;
gridOption = gridBox.Value;
legendLocation = legendLocationDropDown.Value;
legendOrientation = legendOrientationDropDown.Value;
exportPDF = exportPDFBox.Value;
exportFIG = exportFIGBox.Value;
satelliteMapColors = satelliteMapColorsBox.Value;
% figurePath = pathField.Value;
WHratio = WHratioField.Value;
overwriteFigure = overwriteFigureBox.Value;
% Check for required inputs
if isempty(figName) || isempty(percTextwidth)
uialert(fig, 'Please enter both Figure Name and Percentage Text Width.', 'Input Error');
return;
end
% Try to export figure
try
% Call the exportStandardizedFigure function
selectedIdx = figSelectionDropDown.Value;
figToExp = figHandles(strcmp(figSelectionDropDown.Items, selectedIdx));
exportStandardizedFigure(figToExp, figName, percTextwidth, ...
'addMarkers', addMarkers, ...
'forcedMarkers', forcedMarkers, ...
'changeColors', changeColors, ...
'changeLineStyle', changeLineStyle, ...
'grid', gridOption, ...
'legendLocation', legendLocation, ...
'legendOrientation', legendOrientation, ...
'exportPDF', exportPDF, ...
'exportFIG', exportFIG, ...
'satelliteMapColors', satelliteMapColors, ...
'WHratio', WHratio, ...
'overwriteFigure', overwriteFigure);
uialert(fig, 'Figure exported successfully!', 'Success', 'Icon', 'success');
catch ME
% Display error if the function fails
uialert(fig, ['Error exporting figure: ' ME.message], 'Error');
end
end
function previewFigureCallback()
% Get values from the GUI components
percTextwidth = percTextField.Value;
addMarkers = addMarkersBox.Value;
forcedMarkers = forcedMarkersField.Value;
changeColors = changeColorsBox.Value;
changeLineStyle = changeLineStyleBox.Value;
gridOption = gridBox.Value;
legendLocation = legendLocationDropDown.Value;
legendOrientation = legendOrientationDropDown.Value;
exportPDF = exportPDFBox.Value;
exportFIG = exportFIGBox.Value;
satelliteMapColors = satelliteMapColorsBox.Value;
% figurePath = pathField.Value;
WHratio = WHratioField.Value;
overwriteFigure = overwriteFigureBox.Value;
selectedIdx = figSelectionDropDown.Value;
figToExp = figHandles(strcmp(figSelectionDropDown.Items, selectedIdx));
previewStandardizedFigure(figToExp, percTextwidth, ...
'addMarkers', addMarkers, ...
'forcedMarkers', forcedMarkers, ...
'changeColors', changeColors, ...
'changeLineStyle', changeLineStyle, ...
'grid', gridOption, ...
'legendLocation', legendLocation, ...
'legendOrientation', legendOrientation, ...
'exportPDF', exportPDF, ...
'exportFIG', exportFIG, ...
'satelliteMapColors', satelliteMapColors, ...
'WHratio', WHratio, ...
'overwriteFigure', overwriteFigure);
end
end
function figHandles = findFigures(fig)
figHandles = findall(0, 'Type', 'figure');
figHandles(figHandles == fig) = []; % Exclude GUI figure
end
function figNames = getFigureNames(figHandles)
if isempty(figHandles)
figNames = {'No figures open'};
else
figNames = arrayfun(@(f) sprintf('%s', f.Name), figHandles, 'UniformOutput', false);
% Replace empty names with "Figure #"
emptyNames = cellfun(@isempty, figNames);
figNames(emptyNames) = arrayfun(@(f) ...
sprintf('Figure %d', f.Number), figHandles(emptyNames), 'UniformOutput', false);
end
end
%% Further developments
% File Path Input
% uilabel(fig, 'Text', 'Figure Save Path:', 'Position', [50 70 150 30]);
% pathField = uitextarea(fig, 'Position', [200 70 300 30]);
\ No newline at end of file
...@@ -428,7 +428,7 @@ for k = 1:length(f.Children) % for each subfigure ...@@ -428,7 +428,7 @@ for k = 1:length(f.Children) % for each subfigure
markerIndices = linspace(1,nElements,nForcedMarkers(k)); markerIndices = linspace(1,nElements,nForcedMarkers(k));
lines(Nlines+1-i).MarkerIndices = round(markerIndices,0); lines(Nlines+1-i).MarkerIndices = round(markerIndices,0);
end end
lines(Nlines+1-i).LineWidth = 1.5*mult; % general settings lines(Nlines+1-i).LineWidth = mult*1.5; % general settings
end end
end end
...@@ -437,16 +437,28 @@ for k = 1:length(f.Children) % for each subfigure ...@@ -437,16 +437,28 @@ for k = 1:length(f.Children) % for each subfigure
end end
%% export %% export
% Export PDF
if p.Results.exportPDF if p.Results.exportPDF
s1 = strcat(figPath, figureName,'.pdf'); figPath = p.Results.figurePath;
if ~isempty(figPath) && ~(strcmp(figPath(end), '\') || strcmp(figPath(end), '/'))
figPath = strcat(figPath, '\');
else
figPath = fullfile(fileparts(mfilename("fullpath")), '\');
end
s1 = string(strcat(figPath, figureName, '.pdf'));
if isempty(s1)
error('Invalid file name or path.');
end
if exist(s1,"file")==0 || p.Results.overwriteFigure if exist(s1, 'file') == 0 || p.Results.overwriteFigure
exportgraphics(f,s1,"ContentType","vector") exportgraphics(f, s1, "ContentType", "vector");
else else
time = string(datetime('now', 'Format', 'yyyy-MM-dd-HHmmss')); time = string(datetime('now', 'Format', 'yyyy-MM-dd-HHmmss'));
s2 = strcat(figPath, figureName, time, '.pdf'); s2 = strcat(figPath, figureName, time, '.pdf');
exportgraphics(f,s2,"ContentType","vector") exportgraphics(f, s2, "ContentType", "vector");
warning('figure named ''%s'' instead of ''%s''',s2,s1) warning('figure named ''%s'' instead of ''%s''', s2, s1);
end end
fprintf('Figure saved successfully!\n\ntext to copy:\n') fprintf('Figure saved successfully!\n\ntext to copy:\n')
...@@ -455,6 +467,7 @@ if p.Results.exportPDF ...@@ -455,6 +467,7 @@ if p.Results.exportPDF
disp(latexstr) disp(latexstr)
end end
% Export FIG
if p.Results.exportFIG if p.Results.exportFIG
s1 = strcat(figPath, figureName, '.fig'); s1 = strcat(figPath, figureName, '.fig');
if p.Results.overwriteFigure || exist(s1,"file")==0 if p.Results.overwriteFigure || exist(s1,"file")==0
......
function exportStandardizedFigure(fig, figureName, percTextwidth, varargin)
% exportStandardizedFigure - this function standardizes plot graphics and exports the
% figure as a .pdf file
%
% INPUTS:
% fig - figure variable
% figureName - figure name, string, (note: do not add '.pdf')
% percTextwidth - percentage of the linewitdth as indicated in LaTeX,
% (70% -> 0.7), double
% varargin - optional inputs:
% addMarkers - logic, if true every lines has a different
% marker, default: true
% changeColors - logic, if true the colors of the lines will
% be changed, default: true
% changeLineStyle - logic, if both 'changeLineStyle' and
% 'changeColors' are true the lines
% with the same color will have two
% different line styles, default: false
% WHratio - double, width/height ratio; if 'WHratio' is 0
% the ratio will not change,
% default: 0 (current ratio)
% forcedMarkers - double, number of markers for each line. If it
% is set to 0, all points will have a
% marker; default: 0 (all points)
% grid - logic, true to show grid, false otherwise,
% default: true
% legendLocation - string, location of the legend;
% default: 'southoutside'
% legendOrientation - string, orientation of the legend;
% default: 'horizontal'
% exportPDF - logic, true to export pdf file, default: true
% exportFIG - logic, true to export fig file, defalut: false
% satelliteMapColors - logic, if true, color palette for satellite
% map is chosen, default: false
% figurePath - string, path in which the pdf file and/or the
% fig file will be saved, default: ''
% overwriteFigure - logic, if true it will overwrite the .pdf
% file and the .fig file with the same
% name, default: false
%
% --------------------------------EXAMPLE--------------------------------
% >> exportStandardizedFigure(gcf,'nameFig',0.67, 'forcedMarkers', 6, ...
% 'WHratio', 1)
% Figure saved successfully!
%
% text to copy:
% \includegraphics[width=0.67\textwidth]{<add figure path>\nameFig.pdf}
%------------------------------------------------------------------------
%
% VERSIONS: #0, release, Maria Teresa Cazzola
% #1, update, Riccardo Cadamuro, Maria Teresa Cazzola,
% Marco Marchesi
% some improvements
%
%% check input validity
if percTextwidth>1
error('figure width is larger than the page!')
end
%% Parse input
p = inputParser;
addParameter(p, 'addMarkers', true, @islogical);
addParameter(p, 'forcedMarkers', 0);
addParameter(p, 'changeColors', true, @islogical);
addParameter(p, 'changeLineStyle', false, @islogical);
addParameter(p, 'WHratio', 0);
addParameter(p, 'grid', true, @islogical);
addParameter(p, 'exportPDF', true, @islogical);
addParameter(p, 'exportFIG', false, @islogical);
addParameter(p, 'satelliteMapColors', false, @islogical);
addParameter(p, 'figurePath', '');
addParameter(p, 'overwriteFigure', false, @islogical);
addParameter(p, 'legendLocation', 'southoutside');
addParameter(p, 'legendOrientation', 'horizontal');
parse(p, varargin{:});
%% Recall data
%%% diplay plot config
addMarkers = p.Results.addMarkers; % add different markers to lines
changeColors = p.Results.changeColors; % change lines colors
changeLineStyle = p.Results.changeLineStyle; % to diversify lines of the same color;
% change colors has to be true
legendLocation = p.Results.legendLocation;
legendOrientation = p.Results.legendOrientation;
% width\height ratio
if p.Results.WHratio == 0
changeWHratio = false; % true = change width/height ratio; false = keep the same ratio
else
changeWHratio = true;
WHratio = p.Results.WHratio;
end
% forced markers
if p.Results.forcedMarkers == 0
forcedMarkers = false;
nForcedMarkers = nan;
else
forcedMarkers = true;
nForcedMarkers = p.Results.forcedMarkers*ones(1,length(fig.Children));
end
if (~addMarkers) && (forcedMarkers)
warning('addMarker is false and forcedMarkers is true: markers are not displayed')
end
figPath = p.Results.figurePath;
if ~strcmp(figPath,'') && ~(strcmp(figPath(end),'\') || strcmp(figPath(end),'/'))
figPath = strcat(figPath,'\');
end
%%% dimensions setup
mult = 1.5; % multiplier for .fig file view
textwidthCm = 16.54; % \textwidth length in centimeters
fontsize = 9; % reference font size
fontsizeLegend = fontsize*0.8;
%% colors and options lists
if not(p.Results.satelliteMapColors)
colors = {
'#4658A9' % SkywardBlue
'#D3212D' % AmaranthRed
'#00A86B' % Jade
'#FDAF00' % yellowHoney
'#E94196' % Pink
'#FF5A36' % PortlandOrange
'#6CC407' % AppleGreen
'#B100FF' % violet
'#88ACE0' % LightCobalt
};
else
colors = {
'#D3212D' % AmaranthRed
'#FDAF00' % yellowHoney
'#E94196' % Pink
'#6CC407' % AppleGreen
'#B100FF' % violet
'#FF5A36' % PortlandOrange
'#4658D9' % SkywardBlue (more blue)
'#04B96D' % Jade (lighter)
'#80A9DD' % LightCobalt (darker)
};
end
markers = {
'o'
'<'
'square'
'p'
'+'
'>'
'h'
'diamond'
'v'
'+'
'*'
'^'
'x'};
linestylesList = {'-'
'--';
':';
'-.'};
nColors = length(colors);
nMarkers = length(markers);
nStyles = length(linestylesList);
%% figure
f = fig; % figure
widthPos = textwidthCm*percTextwidth*mult;
if ~changeWHratio
WHratio = f.Position(3)/f.Position(4); % retrieve current WHratio
end
heightPos = widthPos/WHratio;
f.Units = "centimeters";
f.Position(3:4) = [widthPos heightPos];
%% check if figure is a tiledchart
if isa(f.Children,'matlab.graphics.layout.TiledChartLayout')
f = fig.Children;
end
%%
for k = 1:length(f.Children) % for each subfigure
if strcmp(f.Children(k).Type, 'uicontextmenu')
continue; % To avoid errors on ContextMenu
end
ax = f.Children(k); % axes
% no minor grid
if isfield(ax,'MinorGridLineStyle')
ax.MinorGridLineStyle = 'none';
end
% interpreter
listFieldnames = fieldnames(ax);
indexInterpreter = find(contains(listFieldnames,'Interpreter'));
if ~isempty(indexInterpreter)
for i = 1:length(indexInterpreter)
ax.(listFieldnames{indexInterpreter(i)}) = 'latex';
end
end
for i = 1:length(listFieldnames)
try
subfieldNames = fieldnames(ax.(listFieldnames{i}));
indexSubInterpreter = find(strcmp(subfieldNames, 'Interpreter'));
if ~isempty(indexSubInterpreter)
for j = 1:length(indexSubInterpreter)
ax.(listFieldnames{i}).(subfieldNames{indexSubInterpreter(j)}) = 'latex';
end
end
catch
end
end
% fontName
indexFontName = find(contains(listFieldnames,'FontName'));
if ~isempty(indexFontName)
for i = 1:length(indexFontName)
ax.(listFieldnames{indexFontName(i)}) = 'Palatino Linotype';
end
end
% fontSize
indexFontSize = find(contains(listFieldnames,'FontSize'));
if ~isempty(indexFontSize)
removeInd = [find(contains(listFieldnames,'FontSizeMode')); ...
find(contains(listFieldnames, 'FontSizeMultiplier'))];
for i = 1:length(indexFontSize)
if sum(removeInd == indexFontSize(i))==0
ax.(listFieldnames{indexFontSize(i)}) = fontsize*mult;
end
end
end
ax.LineWidth = 0.5;
if isa(ax,'matlab.graphics.illustration.Legend') % check if axes is a legend
leg = ax;
leg.Location = legendLocation;
leg.Orientation = legendOrientation;
leg.FontSize = fontsizeLegend*mult;
while leg.Position(3)>0.8 && leg.NumColumns>1
leg.NumColumns = leg.NumColumns-1;
end
elseif isa(ax, 'matlab.graphics.axis.Axes') || isa(ax, 'matlab.graphics.axis.GeographicAxes')
% grid
if isa(ax, 'matlab.graphics.axis.Axes') && p.Results.grid
ax.XGrid = "on";
ax.YGrid = "on";
ax.ZGrid = "on";
elseif isa(ax, 'matlab.graphics.axis.Axes')
ax.XGrid = "off";
ax.YGrid = "off";
ax.ZGrid = "off";
end
jLines = 0;
jHistograms = 0;
jSurface = 0;
jScatter = 0;
jStair = 0;
jCostantLines = 0;
countColors = 0;
countMarkers = 0;
indexLinesColors = [];
indexSurfaceColors = [];
indexScatterColors = [];
indexStairColors = [];
indexLinesMarkers = [];
indexScatterMarkers = [];
indexStairMarkers = [];
for j = 1:length(ax.Children)
if isa(ax.Children(j),'matlab.graphics.chart.primitive.Line')
jLines = jLines+1; countColors = countColors + 1;
countMarkers = countMarkers + 1;
lines(jLines) = ax.Children(j);
indexLinesColors = [indexLinesColors countColors];
indexLinesMarkers = [indexLinesMarkers countMarkers];
elseif isa(ax.Children(j),'matlab.graphics.chart.decoration.ConstantLine')
jCostantLines = jCostantLines+1;
ax.Children(j).LineWidth = 1*mult;
ax.Children(j).Color = [0 0 0];
constLines(jCostantLines) = ax.Children(j);
elseif changeColors && isa(ax.Children(j),'matlab.graphics.chart.primitive.Histogram')
jHistograms = jHistograms+1;
histgrams(jHistograms) = ax.Children(j);
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Surface')
jSurface = jSurface + 1; countColors = countColors + 1;
surfaces(jSurface) = ax.Children(j);
indexSurfaceColors = [indexSurfaceColors countColors];
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Scatter')
jScatter = jScatter + 1; countColors = countColors + 1;
countMarkers = countMarkers + 1;
scatters(jScatter) = ax.Children(j);
indexScatterColors = [indexScatterColors countColors];
indexScatterMarkers = [indexScatterMarkers countMarkers];
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Stair')
jStair = jStair + 1; countColors = countColors +1;
countMarkers = countMarkers + 1;
stairs(jStair) = ax.Children(j);
indexStairColors = [indexStairColors countColors];
indexStairMarkers = [indexStairMarkers countMarkers];
end
end
existLegend = isfield(ax,'Legend');
if existLegend
leg = ax.Legend;
leg.Location = legendLocation;
leg.Orientation = legendOrientation;
leg.FontSize = fontsizeLegend*mult;
while leg.Position(3)>0.8
leg.NumColumns = leg.NumColumns-1;
end
end
%% change graphics
existLines = ~isempty(indexLinesColors);
existSurfaces = ~isempty(indexSurfaceColors);
existScatters = ~isempty(indexScatterColors);
existStairs = ~isempty(indexStairColors);
existConstantLines = exist("constLines", 'var');
existHistograms = exist('histgrams','var');
if existConstantLines
nCLines = length(constLines);
for i = 1:nCLines
index = mod(i-1, nStyles) + 1;
constLines(nCLines+1-i).LineStyle = linestylesList{index};
end
end
if existLines || existSurfaces || existScatters || existStairs || existHistograms
if ~existLines
lines = [];
end
if ~existSurfaces
surfaces = [];
end
if ~existScatters
scatters = [];
end
if ~existStairs
stairs = [];
end
Nlines = length(lines);
Nsurface = length(surfaces);
Nscatter = length(scatters);
Nstair = length(stairs);
%%% change colors
if changeColors
nIndexColors = Nlines + Nsurface + Nscatter + Nstair;
iLines = 0;
iSurface = 0;
iScatter = 0;
iStair = 0;
for i = 1:nIndexColors
index = mod(i-1, nColors) + 1;
if any((nIndexColors+1-i)==indexLinesColors)
iLines = iLines+1;
lines(Nlines+1-iLines).Color = colors{index};
elseif any((nIndexColors+1-i)==indexSurfaceColors)
iSurface = iSurface + 1;
surfaces(Nsurface+1-iSurface).FaceColor = colors{index};
surfaces(Nsurface+1-iSurface).EdgeColor = colors{index};
elseif any((nIndexColors+1-i)==indexScatterColors)
iScatter = iScatter+1;
scatters(Nscatter+1-iScatter).MarkerEdgeColor = colors{index};
scatters(Nscatter+1-iScatter).MarkerFaceColor = 'none';
elseif any((nIndexColors+1-i)==indexStairColors)
iStair = iStair + 1;
stairs(Nstair+1-iStair).Color = colors{index};
end
end
if existHistograms
Nhist = length(histgrams);
for i = 1:Nhist
index = mod(i-1, nColors) + 1;
histgrams(Nhist+1-i).FaceColor = colors{index};
histgrams(Nhist+1-i).EdgeColor = [0 0 0];
end
end
end
%%% addMarkers
if addMarkers
nIndexMarkers = Nlines + Nscatter;
iLines = 0;
iScatter = 0;
for i = 1:nIndexMarkers
index = mod(i-1, nMarkers) + 1;
if any((nIndexMarkers+1-i)==indexLinesMarkers)
iLines = iLines + 1;
lines(Nlines+1-iLines).Marker = markers{index};
lines(Nlines+1-iLines).MarkerSize = mult*3;
else
iScatter = iScatter + 1;
scatters(Nscatter+1-iScatter).Marker = markers{index};
scatters(Nscatter+1-iScatter).LineWidth = mult*1.5;
end
end
end
%%% lines
for i = 1:Nlines
% changeLineStyle
if changeLineStyle
index = mod(i-1, nStyles) + 1;
lines(Nlines+1-i).LineStyle = linestylesList{index};
end
% forcedMarkers
nElements = length(lines(Nlines+1-i).XData);
if forcedMarkers && nElements>nForcedMarkers(k)
markerIndices = linspace(1,nElements,nForcedMarkers(k));
lines(Nlines+1-i).MarkerIndices = round(markerIndices,0);
end
lines(Nlines+1-i).LineWidth = 1.5*mult; % general settings
end
end
clear('lines', 'histgrams', 'surfaces', 'scatters', 'stairs', 'constLines');
end
end
%% export
if p.Results.exportPDF
s1 = strcat(figPath, figureName,'.pdf');
if exist(s1,"file")==0 || p.Results.overwriteFigure
exportgraphics(f,s1,"ContentType","vector")
else
time = string(datetime('now','Format','yyyy-MM-dd-HHmmss'));
s2 = strcat(figPath, figureName, time, '.pdf');
exportgraphics(f,s2,"ContentType","vector")
warning('figure named ''%s'' instead of ''%s''',s2,s1)
end
fprintf('Figure saved successfully!\n\ntext to copy:\n')
latexstr = strcat('\includegraphics[width=',num2str(percTextwidth,2),...
'\textwidth]{<add figure path>\',figureName,'.pdf}');
disp(latexstr)
end
if p.Results.exportFIG
s1 = strcat(figPath, figureName, '.fig');
if p.Results.overwriteFigure || exist(s1,"file")==0
savefig(f,s1)
else
time = string(datetime('now','Format','yyyy-MM-dd-HHmmss'));
s2 = strcat(figPath, figureName, time, '.fig');
savefig(f,s2)
warning('figure saved as ''%s'' instead of ''%s''',s2,s1)
end
end
function previewStandardizedFigure(fig, percTextwidth, varargin)
% exportStandardizedFigure - this function standardizes plot graphics and exports the
% figure as a .pdf file
%
% INPUTS:
% fig - figure variable
% figureName - figure name, string, (note: do not add '.pdf')
% percTextwidth - percentage of the linewitdth as indicated in LaTeX,
% (70% -> 0.7), double
% varargin - optional inputs:
% addMarkers - logic, if true every lines has a different
% marker, default: true
% changeColors - logic, if true the colors of the lines will
% be changed, default: true
% changeLineStyle - logic, if both 'changeLineStyle' and
% 'changeColors' are true the lines
% with the same color will have two
% different line styles, default: false
% WHratio - double, width/height ratio; if 'WHratio' is 0
% the ratio will not change,
% default: 0 (current ratio)
% forcedMarkers - double, number of markers for each line. If it
% is set to 0, all points will have a
% marker; default: 0 (all points)
% grid - logic, true to show grid, false otherwise,
% default: true
% legendLocation - string, location of the legend;
% default: 'southoutside'
% legendOrientation - string, orientation of the legend;
% default: 'horizontal'
% exportPDF - logic, true to export pdf file, default: true
% exportFIG - logic, true to export fig file, defalut: false
% satelliteMapColors - logic, if true, color palette for satellite
% map is chosen, default: false
% figurePath - string, path in which the pdf file and/or the
% fig file will be saved, default: ''
% overwriteFigure - logic, if true it will overwrite the .pdf
% file and the .fig file with the same
% name, default: false
%
% --------------------------------EXAMPLE--------------------------------
% >> exportStandardizedFigure(gcf,'nameFig',0.67, 'forcedMarkers', 6, ...
% 'WHratio', 1)
% Figure saved successfully!
%
% text to copy:
% \includegraphics[width=0.67\textwidth]{<add figure path>\nameFig.pdf}
%------------------------------------------------------------------------
%
% VERSIONS: #0, release, Maria Teresa Cazzola
% #1, update, Riccardo Cadamuro, Maria Teresa Cazzola,
% Marco Marchesi
% some improvements
%
%% check input validity
if percTextwidth>1
error('figure width is larger than the page!')
end
%% Parse input
p = inputParser;
addParameter(p, 'addMarkers', true, @islogical);
addParameter(p, 'forcedMarkers', 0);
addParameter(p, 'changeColors', true, @islogical);
addParameter(p, 'changeLineStyle', false, @islogical);
addParameter(p, 'WHratio', 0);
addParameter(p, 'grid', true, @islogical);
addParameter(p, 'exportPDF', true, @islogical);
addParameter(p, 'exportFIG', false, @islogical);
addParameter(p, 'satelliteMapColors', false, @islogical);
addParameter(p, 'figurePath', '');
addParameter(p, 'overwriteFigure', false, @islogical);
addParameter(p, 'legendLocation', 'southoutside');
addParameter(p, 'legendOrientation', 'horizontal');
parse(p, varargin{:});
%% Recall data
%%% diplay plot config
addMarkers = p.Results.addMarkers; % add different markers to lines
changeColors = p.Results.changeColors; % change lines colors
changeLineStyle = p.Results.changeLineStyle; % to diversify lines of the same color;
% change colors has to be true
legendLocation = p.Results.legendLocation;
legendOrientation = p.Results.legendOrientation;
% width\height ratio
if p.Results.WHratio == 0
changeWHratio = false; % true = change width/height ratio; false = keep the same ratio
else
changeWHratio = true;
WHratio = p.Results.WHratio;
end
% forced markers
if p.Results.forcedMarkers == 0
forcedMarkers = false;
nForcedMarkers = nan;
else
forcedMarkers = true;
nForcedMarkers = p.Results.forcedMarkers*ones(1,length(fig.Children));
end
if (~addMarkers) && (forcedMarkers)
warning('addMarker is false and forcedMarkers is true: markers are not displayed')
end
figPath = p.Results.figurePath;
if ~strcmp(figPath,'') && ~(strcmp(figPath(end),'\') || strcmp(figPath(end),'/'))
figPath = strcat(figPath,'\');
end
%%% dimensions setup
mult = 1.5; % multiplier for .fig file view
textwidthCm = 16.54; % \textwidth length in centimeters
fontsize = 9; % reference font size
fontsizeLegend = fontsize*0.8;
%% colors and options lists
if not(p.Results.satelliteMapColors)
colors = {
'#4658A9' % SkywardBlue
'#D3212D' % AmaranthRed
'#00A86B' % Jade
'#FDAF00' % yellowHoney
'#E94196' % Pink
'#FF5A36' % PortlandOrange
'#6CC407' % AppleGreen
'#B100FF' % violet
'#88ACE0' % LightCobalt
};
else
colors = {
'#D3212D' % AmaranthRed
'#FDAF00' % yellowHoney
'#E94196' % Pink
'#6CC407' % AppleGreen
'#B100FF' % violet
'#FF5A36' % PortlandOrange
'#4658D9' % SkywardBlue (more blue)
'#04B96D' % Jade (lighter)
'#80A9DD' % LightCobalt (darker)
};
end
markers = {
'o'
'<'
'square'
'p'
'+'
'>'
'h'
'diamond'
'v'
'+'
'*'
'^'
'x'};
linestylesList = {'-'
'--';
':';
'-.'};
nColors = length(colors);
nMarkers = length(markers);
nStyles = length(linestylesList);
%% figure
f = fig; % figure
widthPos = textwidthCm*percTextwidth*mult;
if ~changeWHratio
WHratio = f.Position(3)/f.Position(4); % retrieve current WHratio
end
heightPos = widthPos/WHratio;
f.Units = "centimeters";
f.Position(3:4) = [widthPos heightPos];
%% check if figure is a tiledchart
if isa(f.Children,'matlab.graphics.layout.TiledChartLayout')
f = fig.Children;
end
%%
for k = 1:length(f.Children) % for each subfigure
if strcmp(f.Children(k).Type, 'uicontextmenu')
continue; % To avoid errors on ContextMenu
end
ax = f.Children(k); % axes
% no minor grid
if isfield(ax,'MinorGridLineStyle')
ax.MinorGridLineStyle = 'none';
end
% interpreter
listFieldnames = fieldnames(ax);
indexInterpreter = find(contains(listFieldnames,'Interpreter'));
if ~isempty(indexInterpreter)
for i = 1:length(indexInterpreter)
ax.(listFieldnames{indexInterpreter(i)}) = 'latex';
end
end
for i = 1:length(listFieldnames)
try
subfieldNames = fieldnames(ax.(listFieldnames{i}));
indexSubInterpreter = find(strcmp(subfieldNames, 'Interpreter'));
if ~isempty(indexSubInterpreter)
for j = 1:length(indexSubInterpreter)
ax.(listFieldnames{i}).(subfieldNames{indexSubInterpreter(j)}) = 'latex';
end
end
catch
end
end
% fontName
indexFontName = find(contains(listFieldnames,'FontName'));
if ~isempty(indexFontName)
for i = 1:length(indexFontName)
ax.(listFieldnames{indexFontName(i)}) = 'Palatino Linotype';
end
end
% fontSize
indexFontSize = find(contains(listFieldnames,'FontSize'));
if ~isempty(indexFontSize)
removeInd = [find(contains(listFieldnames,'FontSizeMode')); ...
find(contains(listFieldnames, 'FontSizeMultiplier'))];
for i = 1:length(indexFontSize)
if sum(removeInd == indexFontSize(i))==0
ax.(listFieldnames{indexFontSize(i)}) = fontsize*mult;
end
end
end
ax.LineWidth = 0.5;
if isa(ax,'matlab.graphics.illustration.Legend') % check if axes is a legend
leg = ax;
leg.Location = legendLocation;
leg.Orientation = legendOrientation;
leg.FontSize = fontsizeLegend*mult;
while leg.Position(3)>0.8 && leg.NumColumns>1
leg.NumColumns = leg.NumColumns-1;
end
elseif isa(ax, 'matlab.graphics.axis.Axes') || isa(ax, 'matlab.graphics.axis.GeographicAxes')
% grid
if isa(ax, 'matlab.graphics.axis.Axes') && p.Results.grid
ax.XGrid = "on";
ax.YGrid = "on";
ax.ZGrid = "on";
elseif isa(ax, 'matlab.graphics.axis.Axes')
ax.XGrid = "off";
ax.YGrid = "off";
ax.ZGrid = "off";
end
jLines = 0;
jHistograms = 0;
jSurface = 0;
jScatter = 0;
jStair = 0;
jCostantLines = 0;
countColors = 0;
countMarkers = 0;
indexLinesColors = [];
indexSurfaceColors = [];
indexScatterColors = [];
indexStairColors = [];
indexLinesMarkers = [];
indexScatterMarkers = [];
indexStairMarkers = [];
for j = 1:length(ax.Children)
if isa(ax.Children(j),'matlab.graphics.chart.primitive.Line')
jLines = jLines+1; countColors = countColors + 1;
countMarkers = countMarkers + 1;
lines(jLines) = ax.Children(j);
indexLinesColors = [indexLinesColors countColors];
indexLinesMarkers = [indexLinesMarkers countMarkers];
elseif isa(ax.Children(j),'matlab.graphics.chart.decoration.ConstantLine')
jCostantLines = jCostantLines+1;
ax.Children(j).LineWidth = 1*mult;
ax.Children(j).Color = [0 0 0];
constLines(jCostantLines) = ax.Children(j);
elseif changeColors && isa(ax.Children(j),'matlab.graphics.chart.primitive.Histogram')
jHistograms = jHistograms+1;
histgrams(jHistograms) = ax.Children(j);
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Surface')
jSurface = jSurface + 1; countColors = countColors + 1;
surfaces(jSurface) = ax.Children(j);
indexSurfaceColors = [indexSurfaceColors countColors];
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Scatter')
jScatter = jScatter + 1; countColors = countColors + 1;
countMarkers = countMarkers + 1;
scatters(jScatter) = ax.Children(j);
indexScatterColors = [indexScatterColors countColors];
indexScatterMarkers = [indexScatterMarkers countMarkers];
elseif isa(ax.Children(j), 'matlab.graphics.chart.primitive.Stair')
jStair = jStair + 1; countColors = countColors +1;
countMarkers = countMarkers + 1;
stairs(jStair) = ax.Children(j);
indexStairColors = [indexStairColors countColors];
indexStairMarkers = [indexStairMarkers countMarkers];
end
end
existLegend = isfield(ax,'Legend');
if existLegend
leg = ax.Legend;
leg.Location = legendLocation;
leg.Orientation = legendOrientation;
leg.FontSize = fontsizeLegend*mult;
while leg.Position(3)>0.8
leg.NumColumns = leg.NumColumns-1;
end
end
%% change graphics
existLines = ~isempty(indexLinesColors);
existSurfaces = ~isempty(indexSurfaceColors);
existScatters = ~isempty(indexScatterColors);
existStairs = ~isempty(indexStairColors);
existConstantLines = exist("constLines", 'var');
existHistograms = exist('histgrams','var');
if existConstantLines
nCLines = length(constLines);
for i = 1:nCLines
index = mod(i-1, nStyles) + 1;
constLines(nCLines+1-i).LineStyle = linestylesList{index};
end
end
if existLines || existSurfaces || existScatters || existStairs || existHistograms
if ~existLines
lines = [];
end
if ~existSurfaces
surfaces = [];
end
if ~existScatters
scatters = [];
end
if ~existStairs
stairs = [];
end
Nlines = length(lines);
Nsurface = length(surfaces);
Nscatter = length(scatters);
Nstair = length(stairs);
%%% change colors
if changeColors
nIndexColors = Nlines + Nsurface + Nscatter + Nstair;
iLines = 0;
iSurface = 0;
iScatter = 0;
iStair = 0;
for i = 1:nIndexColors
index = mod(i-1, nColors) + 1;
if any((nIndexColors+1-i)==indexLinesColors)
iLines = iLines+1;
lines(Nlines+1-iLines).Color = colors{index};
elseif any((nIndexColors+1-i)==indexSurfaceColors)
iSurface = iSurface + 1;
surfaces(Nsurface+1-iSurface).FaceColor = colors{index};
surfaces(Nsurface+1-iSurface).EdgeColor = colors{index};
elseif any((nIndexColors+1-i)==indexScatterColors)
iScatter = iScatter+1;
scatters(Nscatter+1-iScatter).MarkerEdgeColor = colors{index};
scatters(Nscatter+1-iScatter).MarkerFaceColor = 'none';
elseif any((nIndexColors+1-i)==indexStairColors)
iStair = iStair + 1;
stairs(Nstair+1-iStair).Color = colors{index};
end
end
if existHistograms
Nhist = length(histgrams);
for i = 1:Nhist
index = mod(i-1, nColors) + 1;
histgrams(Nhist+1-i).FaceColor = colors{index};
histgrams(Nhist+1-i).EdgeColor = [0 0 0];
end
end
end
%%% addMarkers
if addMarkers
nIndexMarkers = Nlines + Nscatter;
iLines = 0;
iScatter = 0;
for i = 1:nIndexMarkers
index = mod(i-1, nMarkers) + 1;
if any((nIndexMarkers+1-i)==indexLinesMarkers)
iLines = iLines + 1;
lines(Nlines+1-iLines).Marker = markers{index};
lines(Nlines+1-iLines).MarkerSize = mult*3;
else
iScatter = iScatter + 1;
scatters(Nscatter+1-iScatter).Marker = markers{index};
scatters(Nscatter+1-iScatter).LineWidth = mult*1.5;
end
end
end
%%% lines
for i = 1:Nlines
% changeLineStyle
if changeLineStyle
index = mod(i-1, nStyles) + 1;
lines(Nlines+1-i).LineStyle = linestylesList{index};
end
% forcedMarkers
nElements = length(lines(Nlines+1-i).XData);
if forcedMarkers && nElements>nForcedMarkers(k)
markerIndices = linspace(1,nElements,nForcedMarkers(k));
lines(Nlines+1-i).MarkerIndices = round(markerIndices,0);
end
lines(Nlines+1-i).LineWidth = mult*1.5; % general settings
end
end
clear('lines', 'histgrams', 'surfaces', 'scatters', 'stairs', 'constLines');
end
end