Clear Filters
Clear Filters

How to make auto generate variable name and save to .jpg format?

4 views (last 30 days)
i have code like this :
global datasetNumber;
img = getframe(gca);
baseFileName = sprintf('%d.jpg', datasetNumber);
filename = fullfile(fullfile('H:\SKRIPSI\Citra Latih 2\', baseFileName));
imwrite(rgb2gray(img.cdata),filename,'jpg');
datasetNumber = datasetNumber +1; % Increment for next time.
I want to save an image from an axes, when I save by using the code above, each of the image does not have a file name. I want to save the image that have a file name like 1.jpg, 2.jpg, 3.jpg, and so on without any restrictions in the store a lot of images. how can I make that?
  2 Comments
Adam
Adam on 13 Jun 2016
Edited: Adam on 13 Jun 2016
What do you mean by "doesn't have a filename"? At a glance that looks like it should work, although why datasetNumber is global is questionable and it depends how you are looping round this since you didn't include that code. I assume datasetNumber just parachutes in from somewhere initially, but if you are using a global in the middle of a for loop then it may well just use the same number each time. I never use globals because they are very bad programming style so I don't remember how they would behave like that.
Alvindra Pratama
Alvindra Pratama on 13 Jun 2016
I mean, every time I save drawings that I save do not have a name, as shown below:
so what should I do with the global?

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 13 Jun 2016
Here is a simple example for variable-based file names:
nRange = 1:3;
for n = nRange
plot(rand(7))
filename = ['test ',num2str(n),'.jpg'];
print('-djpeg',filename)
end
  5 Comments
mahrukh jamil
mahrukh jamil on 2 Oct 2016
Thank you for your answer. well i can only name it with numbers. What syntax should i use if I want to develop a function for the extraction of files and then name those file according to numbers. for example. i have 10 subjects. i want to run a function foe each subject and the resulting file should have a name with the number of subject.
function ECG_extraxt(ECG_data, subject_number)
N_max=length(ECG_data);
fs=256;
time = (0:N_max-1)/fs;
EKG_subject_number=[time; ECG_data];
% save('EKG_subject_number.mat', 'EKG_subject_number')
end
how can i have this subject number with my final file name. Or the same condition apply for it too as it were for variables with numbers in a loop?
Kodali
Kodali on 6 Mar 2018
Try this when subject and number have values filename=strcat('EKG_',num2str(subject),'_',num2str(number),'.mat'); save(filename)

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!