matlab绘图(6):模块化图像保存

  1. 复制粘贴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信息执行不同模块,使用者需要根据自己需要调整。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值