Save files using MATLAB

Hi,
I have used a function like below.
function rough1(src,des)
p = dir(fullfile(src,'*.tif'));
mkdir(des);
....
...
...
end
and in the command window i was calling this
  • src= [uigetdir('')];
  • des = uiputfile;
  • rough1(src,des)I can select any folder for processing. I can select any directory for save and can give a folder name. However; it by default saves in MATLAB. As for example if I want to save this to desktop and select it, it still saves in MATLAB.

 Accepted Answer

Siam - uiputfile is used for choosing the filename and directory where you want to save the new file. If you want the path (as well as the file name) you would have to do something like
[filename,pathname] = uiputfile;
But since your are moving/copying multiple files, you don't need to choose a file name. So I think that you want to re-use the call to uigetdir to set the destination folder. Try the following
srcPath = uigetdir('');
destPath = uigetdir('');
rough1(srcPath, destPath)
I tried this, and was able to save all tifs from one directory to a folder on my desktop.

10 Comments

Siam
Siam on 4 Sep 2014
Edited: Siam on 4 Sep 2014
Thank you for your suggestion.
How to use pathname using [filename,pathname] = uiputfile;
But I can not create a new folder while using destPath = uigetdir(''); A new window opens to select a folder but not to create a new folder.
Either I need to use an existing file I need to right click on the window to create a new file. Pleas do correct me if I am wrong.
Is there any way I can create a new folder using the above code?
Thanks a lot.
for me uigetdir() opens up the windows folder selection screen which at the top has a button to make a "New folder" under the address bar (windows 7).
Got it. Thanks a lot.
Siam
Siam on 5 Sep 2014
Edited: Siam on 5 Sep 2014
I can create a new folder anywhere and can save the files in that folder. However; as for example, if a folder already exist and some files are there and I want to save new files in that existing folder it replaces all the existing files with the new files if the file names are same.
Is that possible to add something (as for example a number probably or may be a word ) with the new file and save it in the existing folder rather than replacing the old file with same title from that folder?
Any help will be appreciated.
You can use the exist function to see if the file already exists. If it does, then just rename the file
fileName = sprintf('image_%d.jpg',d);
fullFileName = fullfile(folderName,fileName);
fileId = 1;
while exist(fileName,'file')==2
% create the new file name
fileName = sprintf('image_%d_%d.jpg',d,fileId);
fullFileName = fullfile(folderName,fileName);
% increment the unique identifier
fileId = fileId + 1;
end
The above will check for the file named (for example) image_1.jpg in the specified directory. If found, it will check for the file named image_1_1.jpg, image_1_2.jpg, etc. until the loop condition evaluates to false on a file that does not exist in the folder.
EDIT
The condition in the above while loop should be
while exist(fullFileName,'file')==2
Siam
Siam on 5 Sep 2014
Edited: Siam on 5 Sep 2014
I was trying this what you have suggested me.
If the folder in empty it gives a name like image_1_1 instead of image_1. If I try to save again in the same folder it does not change but replaces the old file.
No clue what I was doing wrong.
Look forward to hear some suggestion from you.
There is a mistake in my above code - I will bold it to indicate the problem. The condition for the while loop should be
while exist(fullFileName,'file')==2
That is what I was looking for. Thanks a lot. Great help.
Siam
Siam on 14 Oct 2014
As for example if my input image is (lets say : image-10-00, image-10-01, image_10_02 >>> now I can save them as process_1, process_2,process_3 serially but not exactly as the same input image title).
Any advice will be appreciated.
Siam - I'm not really sure what you are asking. As this doesn't really correspond to the original question, you should post a new one. Please make it absolutely clear what you are looking for.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 4 Sep 2014

Commented:

on 14 Oct 2014

Community Treasure Hunt

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

Start Hunting!