- 复制粘贴plot_save.m函数
直接放在每个绘图函数的末尾即可,具体示例可见帖子matlab绘图(4)和(5)
function plot_save(plot_info)
% 添加坐标轴标题
% 构建带有单位的标题字符串
% 检查 table 中是否存在所有必需的变量
if all(ismember('units', plot_info.Properties.VariableNames))
xLabelStr = sprintf('%s (%s)', plot_info.xLabel, plot_info.units);
yLabelStr = sprintf('%s (%s)', plot_info.yLabel, plot_info.units);
else
% 如果缺少变量
xLabelStr = sprintf('%s ', plot_info.xLabel);
yLabelStr = sprintf('%s ', plot_info.yLabel);
end
% 设置坐标轴标签
if ismember('tick_x', plot_info.Properties.VariableNames) && plot_info.tick_x == 0
disp('不标注横轴');
else
xlabel(xLabelStr, 'FontName', 'Times New Roman', 'FontSize', 8);
end
ylabel(yLabelStr, 'FontName', 'Times New Roman', 'FontSize', 8);
% 有标题需要输入日期
if ismember('title', plot_info.Properties.VariableNames)
title(plot_info.title);
end
% 美化图形
% grid on;% 网格
box on; % 坐标图框封闭
%% 绘制辅助信息及图像保存
% 检查表格中是否存在 'save' 列,并且其值为 0
if ismember('save', plot_info.Properties.VariableNames) && plot_info.save == 0
% 不保存图像
disp('图像未保存');
else
imageName = [plot_info.imageName, '.tiff']; % 默认使用png格式
fullPath = fullfile(plot_info.filePath, imageName);
% 设置要保存的分辨率
resolution = 300; % 例如,300 DPI
% 保存为指定分辨率的 JPEG 图像
print(gcf, fullPath, '-dtiff', ['-r' num2str(resolution)]);
end
hold off;
%关闭图形窗口
close(gcf);
end
注意代码会根据提供的plot_info信息执行不同模块,使用者需要根据自己需要调整。