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

조회 수: 5 (최근 30일)
Alvindra Pratama
Alvindra Pratama 2016년 6월 13일
댓글: Kodali 2018년 3월 6일
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.
Alvindra Pratama
Alvindra Pratama 2016년 6월 13일
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
mahrukh jamil
mahrukh jamil 2016년 10월 2일
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 2018년 3월 6일
Try this when subject and number have values filename=strcat('EKG_',num2str(subject),'_',num2str(number),'.mat'); save(filename)

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by