Matla学习:figure+axes+plot

时间:2024-05-27 13:04:08
function fig = SetDrawParam()

    %.获得屏幕尺寸
    figpos = , 'ScreenSize');%获得屏幕尺寸,单位像素

    %.设置坐标系在画布中的位置,针对不同尺寸或不同分辨的屏幕需要动态调整才能达到预期的效果
    axex = ;%坐标系原点在画布中的横坐标,单位像素
    axey = ;%坐标系原点在画布中的纵坐标,单位像素
    axew = ;%坐标系的宽度,单位像素
    axeh = ;%坐标系的高度,单位像素

    %.设置纸张在画布中的位置,需要坐标系位置确定的情况才能确定纸张的位置,也需要动态调整才能达到预期的效果
    paperx = ;%纸张原点在画布中的横坐标,单位厘米
    papery = ;%纸张原点在画布中的纵坐标,单位厘米
    paperw = ;%纸张的宽度,单位厘米
    paperh = ;%纸张的高度,单位厘米

    %.设置运行过程中是否显示图像
    visible = 'off';

    %.建立画布和坐标系
    fig = figure('Position', figpos, 'PaperPosition', [paperx, papery, paperw, paperh], 'Visible', visible);
    axe = axes('Units', 'Pixel', 'Position', [axex, axey, axew, axeh]);
end
function SetDrawPlot(x, y, style, width, color, marker)
plot(x, y, 'LineStyle', style, 'LineWidth', width, 'Color', color, 'Marker', marker, 'MarkerEdgeColor', color);
end
function Main_SetDrawPlot_Example()
clear;clc;
folder = '.';
files = dir(strcat(folder, '/*.txt'));
len = length(files);

for i = 1:len
    %1.获得文件名
    indexs = strfind(files(i).name, '.');
    lastIndex = indexs(end);
    name = files(i).name(1:lastIndex-1);

    %2.确定输入输出
    inpath = strcat(folder, '/', files(i).name);
    outpath = strcat(folder, '/', name, '.png');

    %3.提取数据并确定范围
    txt = importdata(inpath);
    x = 1:1:length(txt.data);

    %4.绘图并保存
    fig = SetDrawParam();
    y = txt.data(:,1);
    SetDrawPlot(x, y, '-', 0.5, [0, 0, 1], 'none');
    hold on;
    y = txt.data(:,2);
    SetDrawPlot(x, y, '-', 0.5, [0, 1, 0], 'none');
    hold on;
    y = txt.data(:,3);
    SetDrawPlot(x, y, '-', 0.5, [1, 0, 0], 'none');
    saveas(fig, outpath);
    close all;
end

end