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

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

Adam
Adam 2016년 6월 13일
편집: Adam 2016년 6월 13일
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.
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2016년 6월 13일
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

if i use nRange = 1:3, it means that I could only save three pictures only. Here I want to save the image as much as possible without limitation, how can I do?
Hello, I have some how similar question. What if i want to extract the part of my data and save it as .mat file incremented number. for example
ECG_data_sub1=ECG(data);
do something
ECG_data_1;
ECG_data_2;
....
ECG_data_n;
now can anyone tell me how can i generate and save this incremented file name ECG_sub1_1, ECG_sub1_2... ECG_sub1_n.
Thank you
Bad idea mahrukh. See the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F Incrementing filenames (string contents) is okay, like the cyclist showed, but not variable names. You can extract the parts of the array into a new array or a single variable, and then save that to one or more mat files, and that's okay, but don't make new variable names that have the index built into the name itself.
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?
Try this when subject and number have values filename=strcat('EKG_',num2str(subject),'_',num2str(number),'.mat'); save(filename)

댓글을 달려면 로그인하십시오.

카테고리

질문:

2016년 6월 13일

댓글:

2018년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by