How do you use App designer to read files from a folder and then present those files in the gui for the user to select one or multiple from?

85 views (last 30 days)
Hi, I am very new to the App designer and creating gui's. I already have existing code for a system but now I wish to create a gui from it and in turn make the system more useable and flexible.
I wish for the gui to read in a set of files from a specific folder and then display those files as tick boxes so the user can tick one or more of the files. The ticked files will then be used in future functions. How do I do this?
The files and number of files will essentially be an input into the top-level function.
Thanks

Answers (2)

Image Analyst
Image Analyst on 14 Dec 2022
You need to create a listbox where the user can click on the files to process. Then in the opening code, load up the listbox with code like this.
% Specify top level folder and file pattern.
topLevelFolder = pwd; % Or whatever you want.
filePattern = fullfile(topLevelFolder, '*.dat')
% Get a list of all files in that folder and within its subfolders.
ds = fileDatastore(filePattern, 'ReadFcn', @readmatrix)
allFileNames = ds.Files
app.Listbox.Text = allFileNames; % Or maybe it's app.Listbox.String -- I don't recall.
Then in your "Go!" button, or however you start your batch process, you need to get the Value of the listbox to determine which items were selected, and get the Text property to get all the file names, then loop over selected indexes getting the current data filename and doing something with it.

Voss
Voss on 14 Dec 2022
Here's a function (select_files) you can refer to when working on your app (or use this as-is).
It uses a uitable and a uicontrol. You can use a uitable in an appdesigner app, but you can't use a uicontrol; you'd have to use a uibutton (of style 'push') instead.
Example usage:
% call select_files with all the files in the current directory:
files = dir('*.*');
files([files.isdir]) = [];
files_to_use = select_files({files.name});
The function code:
function selected_files = select_files(files)
if ischar(files)
files = {files};
end
files = files(:);
Nfiles = numel(files);
f = figure( ...
'Name','Select File(s)', ...
'NumberTitle','off', ...
'HandleVisibility','off', ...
'IntegerHandle','off', ...
'DockControls','off', ...
'Toolbar','none', ...
'Menubar','none');
f_pos = get(f,'Position');
t = uitable( ...
'Parent',f, ...
'Units','normalized', ...
'Position',[0 0.2 1 0.8], ...
'Data',[num2cell(false(Nfiles,1)) files], ...
'ColumnEditable',[true false], ...
'ColumnWidth',{25 f_pos(3)-42}, ...
'ColumnName',{'' 'File'}, ...
'ColumnFormat',{'logical' 'char'}, ...
'RowName',{});
uicontrol( ...
'Parent',f, ...
'Units','normalized', ...
'Position',[0.8 0.05 0.15 0.1], ...
'Style','pushbutton', ...
'String','OK', ...
'FontSize',10, ...
'Callback',@cb_ok);
selected_files = {};
waitfor(f);
function cb_ok(~,~)
data = get(t,'Data');
selected_files = files([data{:,1}]);
delete(f);
end
end
  2 Comments
Voss
Voss on 19 Nov 2024 at 15:54
function select_all(~,~)
    t.Data(:,1) = {true};
end

That function should be nested inside the select_files function (like cb_ok is), and it can be a callback of a "select all" button or other control you add.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!