How to make auto generate variable name and save to .jpg format?
4 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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
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
2018-3-6
Try this when subject and number have values filename=strcat('EKG_',num2str(subject),'_',num2str(number),'.mat'); save(filename)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!