hello, i have images being generated after every loop and i want to store them all in one folder only. but with the saveas option, the images get overrriden to those already in the folder. do you guys know how i can fix this? thank you. i am using the following code right now:
h = findobj('type','figure');
n = length(h);
for k=1:n
saveas(figure(k),fullfile('C:\Users\Desktop\project\A data\SegmentedCharWithBlanks',['figure' num2str(k) '.jpg']));
end

댓글 수: 7

Adam
Adam 2017년 6월 12일
That looks like it should create n distinct files. They will overwrite any older files of the same names though, of course. Is that what you mean or are you saying that each of the n figures overwrites the file of the previous one?
shru s
shru s 2017년 6월 12일
I am sorry for not being clear enough. I want to save the new files in such a way that my older files arent erased. suppse i had figure1 , figure2, figure3 in my folder already, i wish to save my new files as figure4, figure5 and so on.
Adam
Adam 2017년 6월 12일
In general it is simple enough to devise an algorithm to reuse freed up numbers in a sequence, but is that what you want to do always - e.g. if you have figure1, figure2, figure4, figure5 already saved and you now want to save 3 new figures. Do you expect these to be named figure3, figure6 and figure7?
That can get quite complicated when you have to keep searching the file list and extracting the numbers off the end of filenames even before you apply such an algorithm.
If it doesn't really matter to you what they are called so long as they are different I would suggest attaching a random number to the end instead. This will almost always be unique, but you should still run it in a loop, creating a random number and checking if the file with that number exists before saving. If it does then take the next random number, etc. The range of such random numbers would depend how many files you expect to have. You can scale your random numbers to any range you like so long as it is big enough that clashes are highly unlikely.
shru s
shru s 2017년 6월 12일
편집: shru s 2017년 6월 12일
it doesnt matter what they are called, but I need them to be saved in the order in which they have been generated because ill be using them in a later stage. so its really important to me to save them in a sequence. if they are named figure1, figure2, etc they wont get jumbled up. if there was a way to save them as A,B,C also its awesome
Adam
Adam 2017년 6월 12일
편집: Adam 2017년 6월 13일
Can you not keep hold of the names for later on? Or use the date modified property of the files to know the order?
I assume the existing files in the folder could have been created at any time in the past?
So I guess you could just do a one-time scan of all existing files in the folder, break down the filenames in the same way you put them together and find the largest number amongst those already saved. Then just start your new indexing from there instead of from 1.
shru s
shru s 2017년 6월 12일
The same program is generating a number of images per loop and im trying to save them all together. I did try to use the largest number to index but havent been successful yet. Ill try it again.Thank you so much for taking the time to look into my issue. I am grateful
shru s
shru s 2017년 6월 12일
편집: shru s 2017년 6월 12일
got it!

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

 채택된 답변

Image Analyst
Image Analyst 2017년 6월 12일

3 개 추천

Use sprintf() to create the filename. It can be unique for each iteration of your loop. Then use imwrite() if you want to save images. For example:
baseFileName = sprintf('Image #%d.png', k);
fullFileName = fullfile(folder, baseFileName);
imwrite(yourImage, fullFileName);
Use saveas() or save() (instead of imwrite) if you want to save figures - basically screenshots of the axes with tickmarks, labels, title, legends, stuff in the graphical overlay above the image like lines and text, etc.

댓글 수: 2

shru s
shru s 2017년 6월 12일
Thank you so much for always helping me out!
Fawad Chandio
Fawad Chandio 2019년 9월 3일
fullFileName means which file...??
baseFileName .....??

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

추가 답변 (1개)

Malayappan Shridhar
Malayappan Shridhar 2020년 3월 28일

0 개 추천

I wish to write a binary image imbin to a folder with the name shown below.
WriteFileName = "C:\ISI-Kappa\FromWorkBin\Front_1\b_180311788230057_Front.jpg"
My command is imwrite(imbin, WriteFileName)
I get this error: Error using imwrite>parse_inputs (line 523)
A filename must be supplied.
I appreciate any help to resolve this error

댓글 수: 1

Are you sure that folder already exists because I'm not sure imwrite() will create folders automatically, though it might. You also might try using forward slashes. What does this say:
whos imbin
Is it a uint8 image?

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

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

태그

질문:

2017년 6월 12일

댓글:

2020년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by