This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [hFig, hAxes, hCurve] = my_plot_function(hAxes, ... | |
xData, yData, myFigProp, myAxesProp, myCurveProp, xStr, yStr) | |
% A plotting wrapper function. Designed to plot one curve per call. | |
% | |
% INPUTS | |
% hAxes - handle to existing axes (set to 0 to create new figure and axes) | |
% xData - x data to plot (set to [] to not plot) | |
% yData - y data to plot (set to [] to not plot) | |
% myFigProp - structure of figure properties to change from defaults. See | |
% subfunction buildFigureStruct for defaults. Ignored if hAxes == 0. Set | |
% to [] to not change any defaults | |
% myAxesProp - structure of axes properties to change from defaults. See | |
% subfunction buildAxesStruct for defaults. Ignored if hAxes == 0. Set | |
% to [] to not change any defaults | |
% myCurveProp - structure of curve properties to change from defaults. See | |
% subfunction buildCurveStruct for defaults. Ignored if xData == [] or | |
% yData == 0. Set to [] to not change any defaults | |
% | |
% OUTPUTS (All optional) | |
% hFig - handle to figure | |
% hAxes - handle to axes. Store to plot more curves later | |
% hCurve - handle to curve | |
% | |
% Created by Adam Noel, 2016-09-16 | |
% Adapted from function accordPlotMaker.m, part of AcCoRD's MATLAB utilities | |
% https://github.com/adamjgnoel/AcCoRD | |
% | |
% Options to expand | |
% - control for type of plot (e.g., plot3, bar, surf) | |
% Create figure and apply properties if it does not exist | |
if hAxes == 0 | |
figProp = buildFigureStruct(myFigProp); | |
axesProp = buildAxesStruct(myAxesProp); | |
% Create Figure | |
hFig = figure; | |
hFig = applyObjChanges(hFig, figProp); | |
% Create Axes | |
hAxes = axes('Parent',hFig); | |
hAxes = applyObjChanges(hAxes, axesProp); | |
hold(hAxes, 'on'); | |
xlabel(xStr, 'interpreter', 'Latex'); | |
ylabel(yStr, 'interpreter', 'Latex'); | |
else | |
hFig = get(hAxes, 'Parent'); | |
end | |
if ~isempty(xData) && ~isempty(yData) | |
% Load curve properties | |
curveProp = buildCurveStruct(myCurveProp); | |
hCurve = plot(hAxes, xData, yData); | |
hCurve = applyObjChanges(hCurve, curveProp); | |
end | |
end | |
% Set default figure properties and apply changes | |
function figProp = buildFigureStruct(myFigProp) | |
figProp.Color = 'w'; | |
figProp = applyStructChanges(figProp, myFigProp); | |
end | |
% Set default axes properties and apply changes | |
function axesProp = buildAxesStruct(myAxesProp) | |
axesProp = struct(... | |
'Box', 'on', ... % If on, draw complete box outline along axis limits | |
'FontSize', 12, ... | |
'XGrid', 'on', ... | |
'YGrid', 'on', ... | |
'TickLabelInterpreter', 'latex'); | |
axesProp = applyStructChanges(axesProp, myAxesProp); | |
end | |
% Set default curve properties and apply changes | |
function curveProp = buildCurveStruct(myCurveProp) | |
curveProp = struct(... | |
'LineStyle', '-', ... % Line style. Options: '-', '--', ':', '-.', 'none' | |
'LineWidth', 1, ... % Line width | |
'Color', 'black', ... % Line color | |
'Marker', 'o', ... % Marker shape. Options: 'o', '+', '*', '.', 'x', 's', 'd', '^', 'v', '>', '<', 'p', 'h', 'none' | |
'MarkerSize', 6, ... % Marker size | |
'MarkerEdgeColor', 'auto', ... % Marker edge color. Options: 'auto', 'none', 'RGB triplet', 'color string' | |
'MarkerFaceColor', 'none', ... % Marker edge color. Options: 'auto', 'none', 'RGB triplet', 'color string' | |
'DisplayName', ''); % Display string (if legend turned on) | |
curveProp = applyStructChanges(curveProp, myCurveProp); | |
end | |
% Change specified elements of structure | |
function curStruct = applyStructChanges(curStruct, changeStruct) | |
if ~isempty(changeStruct) | |
propFields = fieldnames(changeStruct); | |
numProp = numel(propFields); | |
for j = 1:numProp | |
curStruct.(propFields{j}) = changeStruct.(propFields{j}); | |
end | |
end | |
end | |
% Apply changes to object with handle | |
function h = applyObjChanges(h, changeStruct) | |
if ~isempty(changeStruct) | |
propFields = fieldnames(changeStruct); | |
numProp = numel(propFields); | |
for i = 1:numProp | |
set(h, propFields{i}, changeStruct.(propFields{i})); | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment