Saving figure with current file name in MatLab

纵饮孤独 提交于 2019-11-30 23:48:24

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!

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