How can I get the GUI to browse for a specific file?

Hi. I am new to this Matlab GUI and would love your help.
I have an m-file that runs perfectly but wish to put in into a GUI (using GUIDE) to make it more user-friendly.
In my m-file, I manually insert the pathname for the directory in which the necessary jpeg images are to be found. How do I execute this in the GUI? How can I get the user to be able to select the path/file in which the GUI can extract and use the JPEGs within the selected file? I think I need some kind of "browse the user's computer directory" function but am not sure how to achieve this? And how do I then put this path into the m-file?
Any help and advice would be greatly appreciated.
Sue x

 Respuesta aceptada

Gerd
Gerd el 4 de Jul. de 2011
Hi Sue,
you can use uigetfile to open a dialog where the user can choose the file.
Gerd

11 comentarios

Sue
Sue el 4 de Jul. de 2011
Thanks for the quick response. But I am really very new to GUI and don't understand it much. Hopefully I'll b a quick learner.
How exactly do I use uigetfile within GUIDE? How do I get a specific pushbutton to open up a dialog to be able to choose a file?
S x
Gerd
Gerd el 4 de Jul. de 2011
Do you want to create a GUI for the specific problem or just change your m-file real quick.
Please type [filename, pathname, filerindex] = uigetfile
in the command window and see the result. A diaglog appears and you can choose a file.
With the result pathname you can determine the path of all your jpeg files.
dir(pathname) gives you a struct with all files and subdirectories of the current directory. Now you can use all the jpeg files to be processed.
Sue
Sue el 4 de Jul. de 2011
No, I want to be able to create a GUI which asks a user to select a file from their own PC that contains jpeg images. The user then needs to highlight the file, the pathname etc is then shown in a box. Another pushbutton would then activate my m-file, using the user specified images from their own PC, which averages all the images togeher.
Am I describing this correctly?
S x
Sue
Sue el 4 de Jul. de 2011
Excellent!! I've just put "uigetfile" into the callback section in GUIDE and the pushbutton now brings up the dialogue box! So success as far as this goes! However, it goes down to the JPEG image file itself - how do i get it to stop at the directory which contains the JPEG images and not the particular image itself?
And how to get the answer displayed in the editable text box to the right of the pushbutton?
S x
Gerd
Gerd el 4 de Jul. de 2011
Stopping at the directoy itsself works like the same. Use uigetdir to receive the directory name. Which answer do you want to display?
Sue
Sue el 4 de Jul. de 2011
Thank you. uigetdir is what I was after. The answer I want to display is the pathname to the directory so the user can visually see that they have chosen the right file before they execute the next part.
Gerd
Gerd el 4 de Jul. de 2011
You mean the user has choosen the right directory. The answer of uigetdir is the choosen pathdirectory as a string. Now you can display the string e.g. in a static textbox named txt_directory
pathname = uigetdir
set(txt_directory,'String', pathname);
Sue
Sue el 4 de Jul. de 2011
Yes that is correct. But am not sure what you are trying to get me to do. I am using GUIDE to produce this GUI. I therefore have the pushbuttons and static text boxes etc and which part of the uicontrols do I alter etc (once double clicking on them) do I change etc? Sorry if I am being really dumb here x
Gerd
Gerd el 4 de Jul. de 2011
OK,
take a static textbox and Name("Tag")it txt_directory.
Take a push button and Name("Tag") it cmd_getDir.
Now go to the cmd_getdir_callback function and tell the button what to do when it is clicked.
directoy = uigetdir;
% Now you can set the "string" property of the static textbox.
set(handles.txt_directory,'String',directory);
Sue
Sue el 4 de Jul. de 2011
Erm I have tried this but it only puts the string "set(handles.txt_directory,'String',directory);" into the text box - it doesn't actually change into the pathdirectory of the chosen file once the user makes their choice.
Gerd
Gerd el 4 de Jul. de 2011
Sue can you sent me the .fig file and the .m file using the contact. I think we are talking about different stuff :-)

Iniciar sesión para comentar.

Más respuestas (3)

Ka Mirul
Ka Mirul el 20 de Nov. de 2017
I found a video that help me, it is about creating GUI to browse an image and display the image and its name. It should help you : https://youtu.be/7EmFShs5y9I
Narciso Neves
Narciso Neves el 31 de Jul. de 2014
Sue, there is a typo on directoy = uigetdir; Is missing an "r" in "directory" ... that's why it was failing. :D
This is a simplistic version of what I usually add:
I use two handles for the path: InitialPath: the path where I execute the function, you need to get back there in order to keep on executing it. CurrentPath: the path where the user is currently searching.
I create a listbox object for the user to browse in, where I print the current directory list:
function listbox_Ruta_Callback(hObject, eventdata, handles)
option = hObject.Value;
directories = hObject.String;
cd(handles.CurrentPath);
if strcmp(directories(option,1:2),'. ') || strcmp(directories(option,1:2),'..') % two first options are to go to the higher directory
cd('..');
handles.CurrentPath= pwd;
else
if isempty(strfind(directories(option,:),'.')) %If it is a folder, get inside it (maybe better with isdir() function, but I haven't tried)
cd(directories(option,:));
handles.CurrentPath= pwd;
hObject.Value = 1; % You have to reset the value to prevent some errors.
else
%If it is a file, do wahtever you want with it, or pass the information with a handler to another function
handles.CurrentPath= pwd;
end
end
hObject.String = ls;
cd(handles.InitialPath);
% Update modified information
guidata(hObject, handles);
My coments were originally in Spanish, so sorry if I left anything untranslated or poorly done.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

Sue
el 4 de Jul. de 2011

Respondida:

el 20 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by