Вопрос

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

Это было полезно?

Решение

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top