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

조회 수: 16 (최근 30일)
Sue
Sue 2011년 7월 4일
답변: Ka Mirul 2017년 11월 20일
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

채택된 답변

Gerd
Gerd 2011년 7월 4일
Hi Sue,
you can use uigetfile to open a dialog where the user can choose the file.
Gerd
  댓글 수: 11
Sue
Sue 2011년 7월 4일
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 2011년 7월 4일
Sue can you sent me the .fig file and the .m file using the contact. I think we are talking about different stuff :-)

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

추가 답변 (3개)

Ka Mirul
Ka Mirul 2017년 11월 20일
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 2014년 7월 31일
Sue, there is a typo on directoy = uigetdir; Is missing an "r" in "directory" ... that's why it was failing. :D

Jaime López
Jaime López 2017년 3월 8일
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.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by