MATLAB: Write an image name to particular folder using imwrite

MATLABmatlab gui

Dear users ..
i have been used an bottom in GUI to browse an image from computer .. but when i used imwrite, i can not write that image with its name.. so its not efficient to give name to that image each time .. is there any way to write image with its real name .. thanks
[fname path]=uigetfile('*.*');
if isequal(fname,0) | isequal(path,0)
warndlg('Please select an image from directory ..');
else
fname=strcat(path,fname);
Im2=imread(fname);
imwrite(Im2,'D:\imagefolder','jpeg');

Best Answer

Try this robust (but untested) code:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\Program Files\MATLAB' or wherever...
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullSourceFileName = fullfile(folder, baseFileName)
originalImage = imread(fullSourceFileName);
% Create destination filename
destinationFolder = 'D:\imagefolder';
if ~exist(destinationFolder, 'dir')
mkdir(destinationFolder);
end
% Strip off extenstion from input file
[sourceFolder, baseFileNameNoExtenstion, ext] = fileparts(fullSourceFileName);
% Create jpeg filename. Don't use jpeg format for image analysis!
outputBaseName = [baseFileNameNoExtenstion, '.JPG']
fullDestinationFileName = fullfile(destinationFolder, outputBaseName);
% Write the jpg file. This will convert whatever format you started with to the hated jpg format.
imwrite(originalImage, fullDestinationFileName);
Do not be afraid. It's well commented - just study it a line at a time.