How can I see the contents of the folders in the interactive window with uigetdir?

9 views (last 30 days)
Hi,
I want to use uigetdir in my program in order to select the folder that content all the data I need to analyze. I use it in a GUI, I want to get the name of the folder in order to display it after. I used to use uigetfile but I just managed to get the name of the data that are inside my folder so now I use uigetdir. The good point is that I got the name of my folder but in the interaction window I just see the folder and not their content.
Is there a command as uigetfile('*.txt') to choose to see all files with uigetdir ?
Thank you very much !

Answers (1)

TED MOSBY
TED MOSBY on 4 Jun 2025
Hi Naville,
If all you really need is the folder path , you can pick any file and then discard the file name. For example, to see *.txt files inside every folder, then capture the folder path:
[fileName, folderPath] = uigetfile('*.txt', 'Select any .txt file in your data folder');
if isequal(fileName, 0)
return
end
disp(['You chose folder: ', folderPath]);
If you want a “browse‐folders but still see the files inside” experience, you can drop down to Java. The following snippet launches a standard Swing file chooser where you can see both files and folders, but you can still tell it to return a folder path if you want:
% Create a JFileChooser that starts in the current working directory:
jFC = javaObjectEDT('javax.swing.JFileChooser', pwd);
% Lets you select either files or directories:
jFC.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
% Optional: only allow folder selection if you want:
% jFC.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
% Show the dialog:
status = jFC.showOpenDialog([]);
if status == jFC.APPROVE_OPTION
% Grab the full path (could be a file or a folder, depending on what you clicked)
selected = char(jFC.getSelectedFile.getAbsolutePath);
% If you only want a folder, check if it's a directory:
if isfolder(selected)
folderPath = selected;
else
% If you clicked a file, strip off the filename to get its parent folder
folderPath = fileparts(selected);
end
disp(['Chosen folder: ', folderPath]);
else
return
end
Hope this helps!

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!