image thumbnail
from List Files using Pull-Down Menus and Programmatic GUI by Eliot
A Pull-Down Menu for Listing Files within Folders in Programmatic GUI. GUIDE not required.

load_listbox(dir_path,hObject,hObject_echo)
function load_listbox(dir_path,hObject,hObject_echo)
    % Description:
    % This function uses a Matlab popupmenu to list all the files in a given 
    % directory as well as the directory tree where the files are located.
    % It is useful for programmatic gui building. GUIDE was not used in
    % this example at all.
    % This is a detailed (non-GUIDE) expansion of an example shown here:
    % http://www.mathworks.com/help/matlab/creating_guis/list-box-directory-reader-guide.html
    
    % Inputs: 
    % dir_path - eg. C:\ , the desired directory you want to show in the
    % pulldown menu
    % hObject - eg. listbox1 , the object handle (name) of the pulldown menu 
    % in which you want to display the list of files
    % hObject_echo (OPTIONAL INPUT) - eg. text1 , the object handle (name) of the text
    % display box where you want to show the current directory tree. 
    % 
    
    temp = pwd; % save the current working directory.
    
    % get all the data associated with this object
    handles=get(hObject); 
    
    % add an additional entry to this structure called output
    handles.output=hObject; 
    
    % change directory to the specified directory
    cd (dir_path); 
    
    % store the list of files in that directory 
    dir_struct = dir(dir_path); 
    
    % sort the stored data
    [sorted_names,sorted_index] = sortrows({dir_struct.name}'); 
    
    % send store the sorted information into a specific location in the
    % structure
    handles.file_names = sorted_names; 
    
    % copy the current direcory into the structure
    handles.is_dir = [dir_struct.isdir];
    
    % copy the sorting information into the structure
    handles.sorted_index = sorted_index;
    
    % push all the new information contained in the structure back to the
    % object
    guidata(hObject,handles);
    
    % set the words displayed by the popupmenu to the list of file names
    set(hObject,'String',handles.file_names,...
        'Value',1);
    
    % do this only if we add the optional input parameter
    if nargin == 3 
        
        % set the words in a text box to the current selected directory 
        set(hObject_echo,'String',pwd)
    end
    
    cd (temp); % change directories back to the original location. 
    % (If we don't do this, we'll constantly have to browse back to the
    % location of our .m files)
    
end

Contact us