MATLAB:使用默认名称保存图形 [英] MATLAB: Save figure with default name

查看:136
本文介绍了MATLAB:使用默认名称保存图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行生成数字的matlab脚本.要保存此数字,我使用:

print(h_f,'-dpng','-r600','filename.png')

这意味着如果我每次运行脚本时都没有更改文件名,则图形filename.png将被覆盖. 有没有一种方法可以将图形保存为默认名称,例如untitled.png,然后当脚本运行两次时,它将使新图形成为untitled(1).png而不是覆盖原始图形吗?

解决方案

您可以根据现有文件数创建新的文件名

defaultName = 'untitled';
fileName = sprintf('%s_%d.png', defaultName, ...
   length(dir([defaultName '_*.png'])));

print(h_f,'-dpng','-r600', fileName)

如果文件不在当前工作目录中,请在dir搜索路径中添加文件夹路径.

这将创建一个0索引文件名列表

untitled_0.png
untitled_1.png
untitled_2.png
untitled_3.png
...


您还可以使用 tempname 生成长随机每次迭代的名称.在大多数情况下是唯一的,请参见限制.

print(h_f,'-dpng','-r600', [tempname(pwd) '.png'])

输入参数(示例中为 pwd )为如果您不想将文件保存在 TEMPDIR 中,则需要>

I am running a matlab-script that produces a figure. To save this figure I use:

print(h_f,'-dpng','-r600','filename.png')

What this means is that if I don't change filename for each time I run the script, the figure filename.png will be overwritten. Is there a way to save a figure to a default name, e.g. untitled.png, and then when the script is run twice it will make a new figure untitled(1).png instead of overwriting the original one?

解决方案

You could create a new filename based on the number of existing files

defaultName = 'untitled';
fileName = sprintf('%s_%d.png', defaultName, ...
   length(dir([defaultName '_*.png'])));

print(h_f,'-dpng','-r600', fileName)

Add a folder path to your dir search path if the files aren't located in your current working directory.

This will create a 0-index file name list

untitled_0.png
untitled_1.png
untitled_2.png
untitled_3.png
...


You could also use tempname to generate a long random name for each iteration. Unique for most cases, see section Limitations.

print(h_f,'-dpng','-r600', [tempname(pwd) '.png'])

The input argument (pwd in the example) is needed if you do not want to save the files in your TEMPDIR

这篇关于MATLAB:使用默认名称保存图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆