Saving figure with current file name in MatLab

喜夏-厌秋 提交于 2019-11-30 18:48:13

问题


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.

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.

Example

The plot of the data in data1.mat should be saved in the file data1.jpeg


回答1:


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!




回答2:


Since your script already has the information of the filenames (otherwise it could not open the files and read the data), you could just extend the filename with '.jpg' and pass this string to the saveas function. Demo for filename 'hello':

>> filename = 'hello'
filename =
hello
>> picname = [filename, '.jpg']
picname =
hello.jpg
>> a = figure
a =
 4
>> saveas(a, picname)
>> ls
hello.jpg


来源:https://stackoverflow.com/questions/24046157/saving-figure-with-current-file-name-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!