在MatLab中用当前文件名保存图形 [英] Saving figure with current file name in MatLab
问题描述
我有一个脚本,可以一次从当前工作目录中提取一个文件并绘制指定的信息.我想用绘图文件的名称将每个绘图另存为jpeg(tiff也可以).我大约有3000个文件,因此我正在寻找一种自动化的方法来执行此操作.
I have a script that pulls one file at a time from my current working directory and plots specified information. I would like to save each of the plots as a jpeg (tiff is okay too) with the name of the file it's plotting. I have about 3000 files, so I am looking for an automated way to do this.
我认为如果放在for循环的末尾,这可能会起作用:
I thought this might work if placed at the end of the for-loop:
saveas(gcf, ' ',jpg)
我不确定要在文件名的引号中加上什么.
I am not sure what to put in quotations for the file name.
data1.mat
中的数据图应保存在文件data1.jpeg
The plot of the data in data1.mat
should be saved in the file data1.jpeg
推荐答案
如果loadedFileName
是刚加载的文件的名称(也许是路径),则可以执行以下操作将jpeg保存为相同的文件名
If loadedFileName
is the name (and perhaps) path of the file that you just loaded, then you could do something like the following to save the jpeg with the same file name
% get the path, name and extension of the file just loaded
[path, name, ext] = fileparts(loadedFileName);
% grab what you want to create the filename of the jpeg to save
jpegToSaveFileName = [name '.jpg']; % use path if you want to save to same directory
% save the figure as a jpeg
saveas(gcf,jpegToSaveFileName,'jpg');
尝试以上操作,看看会发生什么.如果您需要在文件名中添加路径,请执行类似的操作
Try the above and see what happens. If you need to add a path to the file name, then do something like
jpegToSaveFileName = fullfile(path, jpegToSaveFileName);
尝试上面的方法,看看它是否满足您的需求!
Try the above and see if it does what you need!
这篇关于在MatLab中用当前文件名保存图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!