how to create a folder in GUI

13 views (last 30 days)
mehdi
mehdi on 15 Oct 2014
Commented: Geoff Hayes on 16 Oct 2014
I'm still new with GUI. I want to create a folder In GUI, each time I push a button. I will also set some images in this folder.
I will actually call a funktion that crop some images. The problem is, when I create a new folder in GUI by pushing a button, the folder is empty and the function therefore does nothing.
I make a funktion for create a folder as below:
function handles = directory( handles )
direct = uigetdir(pwd, 'Select Directory');
handles.myData.direct = direct;
end
a funktion that crop images is:
function handles = Cropbillede( handles )
folder_name = uigetdir;
ImagesToRead = dir(fullfile(folder_name, '*tif'));
ImageCell=cell(length(ImagesToRead),1);
CroppedImageCell=cell(length(ImagesToRead),1);
for k = 1 : length(ImagesToRead)
ImageCell{k} = imread(ImagesToRead(k).name);
% Read image and store in cell array.
CroppedImageCell{k} = imcrop(ImageCell{k},[0 0 1045 791]);
% See comments below
end
SequenceName = 'CroppedBone' ;
for k = 1:length(ImagesToRead)
ExportedName = sprintf('%s%03d.tif',SequenceName,k);
% Add the frame # after the name
imwrite(CroppedImageCell{k}...
,ExportedName,...
'tif','WriteMode','overwrite','Compression','none');
end
for k = 1:length(ImagesToRead)
imwrite(CroppedImageCell{k},handles.myData.direct,SequenceName,...
'tif','WriteMode','append','Compression','none');
% Note the "append" writing mode.
end
end
and in my GUI:
function textfield_Callback(hObject, eventdata, handles)
handles = directory( handles );
guidata(hObject,handles);
handles = Cropbillede( handles );
guidata(hObject,handles);
Anyone knows what is the problem in this code? and how to put images in this folder?
thanks in advance.

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Oct 2014
Mehdi - you need to describe the steps that the user would follow when using your GUI. Does he/she
  • select some files for cropping
  • crop the images
  • press a button to save the cropped images
  • select a (new) folder to those cropped images
Or does the above occur in some other order? You have supplied a callback for a textfield, which seems to have the code for creating a directory, and then crops the images. It is unclear though why or how this textfield is to be used - why should its callback (and not a button?) be used to create the directory and crop the images? As well, are the directory and Cropbillede functions in separate m files, or declared within the GUI?
From your directory function, you do the following
function handles = directory( handles )
direct = uigetdir(pwd, 'Select Directory');
handles.myData.direct = direct;
end
So the user selects a directory (new or existing) and the value gets saved to the handles.myData.direct field. The updated handles gets returned and in the textfield callback the structure is saved using guidata. Then Croppbillede is called and has two blocks of code where data is written
SequenceName = 'CroppedBone' ;
for k = 1:length(ImagesToRead)
ExportedName = sprintf('%s%03d.tif',SequenceName,k);
% Add the frame # after the name
imwrite(CroppedImageCell{k}...
,ExportedName,...
'tif','WriteMode','overwrite','Compression','none');
end
for k = 1:length(ImagesToRead)
imwrite(CroppedImageCell{k},handles.myData.direct,SequenceName,...
'tif','WriteMode','append','Compression','none');
% Note the "append" writing mode.
end
The first block seems to be writing a number of images to file, all whose name is something like CroppedBoneXYZ.tig where XYZ is a three digit number. But you don't specify the directory/folder for these files. Should the folder be that which was selected from the directory function? If so, then replace your ExportedName initialization with
ExportedName = fullfile(handles.myData.direct,sprintf('%s%03d.tif',SequenceName,k));
which will add the directory to ExportedName and so the files should be saved there.
In your second for loop, you don't specify a file per se, but seem to expect that the same image (?) will be appended to the existing file since you are providing the directory to save the file and the SequenceName which is just CroppedBone. I suspect that this block would cause an error to be thrown - are you not observing this?
I suggest that you comment out the second for loop and check to make sure that the first for loop (once updated with the directory/folder to save the files) is working correctly, and then decide whether you need this extra code or not.
  5 Comments
mehdi
mehdi on 16 Oct 2014
yes it's my name on pushbutton. and yes I have modified the code as you said.
Geoff Hayes
Geoff Hayes on 16 Oct 2014
By pushing a button, a folder appeares. By select the folder where images are, the images will be made into croped images in same folder.
Yes, that is correct. If your cropped files are being written to the same directory as the uncropped images, then this is because you have selected to read the files from the same directory as want to write them to.

Sign in to comment.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!