init commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
function hAnnot = arrow(startPoint, endPoint, ax, arrowType, varargin)
|
||||
% ARROW Draws an annotation of specified type between two data points.
|
||||
%
|
||||
% hAnnot = ARROW(startPoint, endPoint, ax, arrowType) creates an
|
||||
% annotation from the coordinate specified by startPoint to the coordinate
|
||||
% specified by endPoint. The annotation type can be 'arrow', 'doublearrow',
|
||||
% 'textarrow', etc.
|
||||
%
|
||||
% hAnnot = ARROW(startPoint, endPoint, ax, arrowType, Name, Value) allows
|
||||
% additional customization of the annotation properties (e.g., color,
|
||||
% arrowhead size, line width, etc.).
|
||||
%
|
||||
% INPUT:
|
||||
% startPoint - 1x2 array, [x1, y1], coordinates of the start point
|
||||
% in the data space of the current axes.
|
||||
% endPoint - 1x2 array, [x2, y2], coordinates of the end point
|
||||
% in the data space of the current axes.
|
||||
% ax - (optional) Axes handle. If omitted, the current axes
|
||||
% (gca) will be used.
|
||||
% arrowType - (optional) Type of annotation ('arrow', 'doublearrow',
|
||||
% 'textarrow', etc.). Default is 'arrow'.
|
||||
% Name, Value - (optional) Name-value pairs to customize the annotation's
|
||||
% appearance (e.g., 'Color', 'LineWidth', etc.).
|
||||
%
|
||||
% OUTPUT:
|
||||
% hAnnot - Handle to the created annotation object.
|
||||
%
|
||||
% Example:
|
||||
% figure;
|
||||
% x = linspace(0, 10, 100);
|
||||
% y = sin(x);
|
||||
% plot(x, y);
|
||||
% arrow([2, sin(2)], [8, sin(8)], gca, 'textarrow', 'Color', 'r', 'String', 'Text');
|
||||
%
|
||||
% Notes:
|
||||
% The function automatically converts the data coordinates to normalized
|
||||
% figure coordinates for annotation purposes.
|
||||
|
||||
% Input validation
|
||||
if nargin < 2
|
||||
error('arrow:InsufficientInputs', 'Both startPoint and endPoint must be specified.');
|
||||
end
|
||||
if nargin < 3 || isempty(ax)
|
||||
ax = gca; % If axes handle is not provided, use the current axes
|
||||
end
|
||||
if nargin < 4 || isempty(arrowType)
|
||||
arrowType = 'arrow'; % Default arrow type is 'arrow'
|
||||
end
|
||||
if numel(startPoint) ~= 2 || numel(endPoint) ~= 2
|
||||
error('arrow:InvalidInput', 'startPoint and endPoint must be 1x2 arrays representing [x, y] coordinates.');
|
||||
end
|
||||
|
||||
% Get the current axes position in normalized figure units
|
||||
ax_pos = ax.Position;
|
||||
|
||||
% Convert data coordinates to normalized figure coordinates
|
||||
startNorm = [(startPoint(1) - ax.XLim(1)) / diff(ax.XLim), ...
|
||||
(startPoint(2) - ax.YLim(1)) / diff(ax.YLim)];
|
||||
endNorm = [(endPoint(1) - ax.XLim(1)) / diff(ax.XLim), ...
|
||||
(endPoint(2) - ax.YLim(1)) / diff(ax.YLim)];
|
||||
|
||||
% Map normalized data coordinates to figure normalized coordinates
|
||||
startAnnot = [ax_pos(1) + startNorm(1) * ax_pos(3), ...
|
||||
ax_pos(2) + startNorm(2) * ax_pos(4)];
|
||||
endAnnot = [ax_pos(1) + endNorm(1) * ax_pos(3), ...
|
||||
ax_pos(2) + endNorm(2) * ax_pos(4)];
|
||||
|
||||
% Create the specified type of annotation
|
||||
hAnnot = annotation(arrowType, [startAnnot(1) endAnnot(1)], ...
|
||||
[startAnnot(2) endAnnot(2)], ...
|
||||
varargin{:}); % Pass additional name-value pairs
|
||||
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
clear; clc; close all;
|
||||
|
||||
fig_config();
|
||||
|
||||
%% Initiate test plot data
|
||||
x = linspace(0, 10, 14);
|
||||
|
||||
% -- bounds
|
||||
upper_bound = ones(size(x)) * 4.5;
|
||||
lower_bound = upper_bound * 0.4
|
||||
|
||||
% -- lines
|
||||
method_1_1 = upper_bound - (upper_bound - lower_bound) .* exp(-0.7 * x);
|
||||
method_1_2 = upper_bound - (upper_bound - lower_bound) .* exp(-0.5 * x);
|
||||
method_1_3 = upper_bound - (upper_bound - lower_bound) .* exp(-0.3 * x);
|
||||
method_2 = upper_bound - (upper_bound - lower_bound) .* exp(-0.5 * x) .* cos(x);
|
||||
|
||||
|
||||
%% Plot
|
||||
x_min = 0; x_max = 10;
|
||||
y_min = 2; y_max = 6;
|
||||
fig = figure(); hold on; box on; grid on;
|
||||
line_optimal = plot(x, upper_bound, 'k-', 'DisplayName', 'Optimal');
|
||||
line_method_1 = plot(x, method_1_1, '^b-', 'DisplayName', 'Method 1');
|
||||
plot(x, method_1_2, '^b--')
|
||||
plot(x, method_1_3, '^b-.')
|
||||
line_method_2 = plot(x, method_2, '*r-', 'DisplayName', 'Method 2');
|
||||
xlim([x_min, x_max]);
|
||||
ylim([y_min, y_max]);
|
||||
xticks(x_min:1:x_max);
|
||||
yticks(y_min:1:y_max);
|
||||
|
||||
% -- Add arrow
|
||||
arrow([2.5, 4.2], [3.8, 3]);
|
||||
text(4, 3, '$\gamma = 1, 2, 3$')
|
||||
|
||||
% -- Add legends and labels
|
||||
% h = findobj(fig, '-regexp', 'DisplayName', '.+');
|
||||
lgd = legend([line_optimal, line_method_1, line_method_2]);
|
||||
set(lgd, 'ItemTokenSize', [10, 5]);
|
||||
ylabel("$\mathbf{E}\left[S\right]$")
|
||||
xlabel("Rounds of Iteration")
|
||||
|
||||
% -- export
|
||||
fig_export('example_matlab.pdf', 'pdf', '../../images')
|
||||
@@ -0,0 +1,72 @@
|
||||
function fig_config(customConfig)
|
||||
% setFigureDefaults Sets default properties for all figures
|
||||
%
|
||||
% Usage:
|
||||
% fig_config() % Applies default settings
|
||||
% fig_config(userConfig) % Overrides defaults with userConfig
|
||||
|
||||
%% Define default settings
|
||||
% In IEEETran, \textwidth is 7.1413 inches
|
||||
% - Use `\printinunitsof{in}\prntlen{\textwidth}`
|
||||
% - To output the \textwidth value in the generated document,
|
||||
% you need to use the `printlen` and `layouts` packages.
|
||||
textwidth_inches = 7.1413;
|
||||
|
||||
% Set the figure width to 0.5 times \textwidth and define the aspect ratio (16:9 or 4:3)
|
||||
aspect = 16/9; % Aspect ratio (can also use 4/3)
|
||||
axes_width = 0.75;
|
||||
axes_hight = 0.75;
|
||||
axes_pos = [0.15, 0.15, axes_width, axes_hight];
|
||||
fig_width = 0.5 * textwidth_inches;
|
||||
fig_hight = (fig_width * axes_width / aspect) / axes_hight;
|
||||
|
||||
defaultConfig = struct( ...
|
||||
'defaultFigureUnits', 'inches', ...
|
||||
'defaultFigurePosition', [0, 0, fig_width, fig_hight], ...
|
||||
'defaultAxesPosition', axes_pos, ...
|
||||
'defaultFigurePaperPositionMode', 'manual', ...
|
||||
'defaultFigurePaperUnits', 'inches', ...
|
||||
'defaultFigurePaperPosition', [0, 0, fig_width, fig_hight], ...
|
||||
'defaultFigurePaperSize', [fig_width, fig_hight], ...
|
||||
'defaultLineLineWidth', 0.5, ... % -- Line and marker
|
||||
'defaultLineMarkerSize', 3, ...
|
||||
'defaultAxesFontName', 'Times New Roman', ... % -- Fonts
|
||||
'defaultTextFontName', 'Times New Roman', ...
|
||||
'defaultLegendFontName', 'Times New Roman', ...
|
||||
'defaultAxesLabelFontSizeMultiplier', 1.15, ...
|
||||
'defaultAxesFontSize', 7, ...
|
||||
'defaultTextFontSize', 10, ...
|
||||
'defaultLegendFontSize', 7, ...
|
||||
'defaultTextInterpreter', 'latex', ... % -- Interpreter
|
||||
'defaultAxesTickLabelInterpreter', 'latex', ...
|
||||
'defaultLegendInterpreter', 'latex', ...
|
||||
'defaultAxesGridColor', [0.15, 0.15, 0.15], ... % -- Grid
|
||||
'defaultAxesGridAlpha', 0.25, ...
|
||||
'defaultAxesGridLineStyle', ':', ...
|
||||
'defaultAxesGridLineWidth', 0.5, ...
|
||||
'defaultArrowshapeHeadWidth', 6, ... % -- Arrow
|
||||
'defaultArrowshapeHeadLength', 6 ...
|
||||
);
|
||||
|
||||
% Merge user config with defaults
|
||||
if nargin == 1 && ~isempty(customConfig)
|
||||
userFields = fieldnames(customConfig);
|
||||
for i = 1:length(userFields)
|
||||
defaultConfig.(userFields{i}) = customConfig.(userFields{i});
|
||||
end
|
||||
end
|
||||
|
||||
% Iterate and set properties
|
||||
fields = fieldnames(defaultConfig);
|
||||
for i = 1:length(fields)
|
||||
try
|
||||
set(groot, fields{i}, defaultConfig.(fields{i}));
|
||||
catch ME
|
||||
errorStruct.message = sprintf("%s\nUse get(groot, 'factory') " + ...
|
||||
"to get all the available parameters.", ME.message);
|
||||
errorStruct.identifier = ME.identifier;
|
||||
errorStruct.stack = ME.stack;
|
||||
error(errorStruct)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,80 @@
|
||||
function fig_export(filename, format, folder, dpi, fig)
|
||||
% EXPORT_CHART Save the current MATLAB figure or a specified figure in the selected image format.
|
||||
%
|
||||
% Parameters:
|
||||
% - filename (string, optional): The output file name. If it has no extension, one will be
|
||||
% added based on the format. If missing or empty, a default
|
||||
% name 'exported_plot' is used.
|
||||
% - format (string): The desired file format for the export. Default is 'pdf'.
|
||||
% Accepted formats are 'png', 'jpg', 'tiff', 'svg', 'pdf', 'esp'.
|
||||
% - folder (string): The directory where the image file will be saved. Default is the current directory.
|
||||
% - dpi (int): Resolution in dots per inch. Default is 96.
|
||||
% - fig (figure handle, optional): The MATLAB Figure object to export. If empty, the current active figure is used.
|
||||
%
|
||||
% Notes:
|
||||
% - The function creates the folder if it doesn't exist.
|
||||
% - Ensures compatibility with supported MATLAB formats.
|
||||
|
||||
if nargin < 1 || isempty(filename)
|
||||
timestamp = char(datetime('now', 'Format','yyyyMMdd_HHmmSS'));
|
||||
filename = ['exported_plot_' timestamp];
|
||||
end
|
||||
if nargin < 2 || isempty(format)
|
||||
format = 'pdf';
|
||||
end
|
||||
if nargin < 3 || isempty(folder)
|
||||
folder = '.';
|
||||
end
|
||||
if nargin < 4 || isempty(dpi)
|
||||
dpi = 96;
|
||||
end
|
||||
if nargin < 5 || isempty(fig)
|
||||
fig = gcf;
|
||||
end
|
||||
|
||||
% Add extension if missing
|
||||
[~, ~, ext] = fileparts(filename);
|
||||
if isempty(ext)
|
||||
filename = [filename '.' format];
|
||||
else
|
||||
format = ext(2:end); % Use extension for format
|
||||
end
|
||||
|
||||
% Validate folder and create if necessary
|
||||
if ~exist(folder, 'dir')
|
||||
mkdir(folder);
|
||||
end
|
||||
|
||||
% Construct full file path
|
||||
filepath = fullfile(folder, filename);
|
||||
|
||||
% Format to '-d' option mapping
|
||||
supportedFormats = struct( ...
|
||||
'png', '-dpng', ...
|
||||
'jpg', '-djpeg', ...
|
||||
'tiff', '-dtiff', ...
|
||||
'svg', '-dsvg', ...
|
||||
'pdf', '-dpdf', ...
|
||||
'eps', '-depsc' ...
|
||||
);
|
||||
if ~isfield(supportedFormats, lower(format))
|
||||
error('Unsupported format "%s". Supported formats are: %s.', ...
|
||||
format, strjoin(fieldnames(supportedFormats), ', '));
|
||||
end
|
||||
|
||||
% Get the corresponding '-d' option
|
||||
dOption = supportedFormats.(lower(format));
|
||||
|
||||
% Attempt to save the figure
|
||||
try
|
||||
% Include resolution for raster formats
|
||||
if ismember(lower(format), {'png', 'jpg', 'tiff'})
|
||||
print(fig, filepath, dOption, ['-r' num2str(dpi)]);
|
||||
else
|
||||
print(fig, filepath, dOption);
|
||||
end
|
||||
fprintf('Exported image to: "%s" as %s\n', filepath, format);
|
||||
catch ME
|
||||
fprintf('Failed to export the figure: %s\n', ME.message);
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user