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?

65 ビュー (過去 30 日間)
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

回答 (2 件)

Image Analyst
Image Analyst 2022 年 12 月 14 日
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 2022 年 12 月 14 日
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 件のコメント
crijn
crijn 2024 年 11 月 19 日
very nice GUI, could you help me add a fuction to selct all the files?
Voss
Voss 2024 年 11 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by