【问题标题】:Save images in a loop with variable file names?使用可变文件名将图像保存在循环中?
【发布时间】:2014-07-10 18:36:53
【问题描述】:

我有一个这样的循环:

for n = 1:6
figure
plot()
saveas(gcf,'figure', 'jpeg')
end

然而,这只是将数字相互保存,因为它们都具有相同的名称。我需要做的是让它的名字是'figure_n',其中n是循环的迭代。

【问题讨论】:

    标签: string matlab numbers save-as


    【解决方案1】:
    for i=1:6
        % construct the filename for this loop - this would be `str1` in your example
        file_name = sprintf('picture_%i.jpeg', i);
        % or:
        file_name = strcat('picture_', num2str(i), '.jpeg');
        % call the function with this filename:
        saveas(gcf,'file_name','jpeg') 
    end
    

    希望这会有所帮助。

    【讨论】:

    • 我喜欢在sprintf 中使用 '%02i' 选项,因此文件编号为 01,02,... 而不是 1,2,... 如果您有超过 10 个文件,当您按文件名对文件夹进行排序时,它们会正确排序。
    【解决方案2】:

    使用num2str

    saveas(gcf, ['figure_' num2str(n) ], 'jpeg') ;
    

    【讨论】:

    • 简单有效!附带问题,我将如何更改那行代码以将所有图像保存到我桌面上的文件夹中?
    • @user3145111 类似saveas(gcf, ['C:\path_to_folder_on_desktop\' 'figure_' num2str(n) ], 'jpeg') ;
    猜你喜欢
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2022-06-12
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多