How to ask the user how many file extensions to search for

1 view (last 30 days)
I currently have a script which find files in a folder with a matching string that the user inputs.
I also have code where the user enters the file extensions he wants the script to look for.
ext1 = inputdlg('Please enter file extension #1: **\*. ');
ext2 = inputdlg('Please enter file extension #2: **\*. ');
ext3 = inputdlg('Please enter file extension #3: **\*. ');
fileData = [dir(fullfile(fileLoc,char(ext1))); dir(fullfile(fileLoc,char(ext2))); dir(fullfile(fileLoc,char(ext3)))];
What I want to do instead of ask the user how many different file extensions he wants to look for and then ask for them accordingly.
(e.g. User inputs he wants to search for 5 different file extensions and the script asks for 5)
I'd like to do this instead of just hard-coding in how many file extensions he can search for.

Accepted Answer

Adam Danz
Adam Danz on 13 Sep 2019
Edited: Adam Danz on 13 Sep 2019
That's too much work (for you and the user). Instead, the user could just list all file extensions in the first dialog, separated by commas. Then you can separate that list into individual extensions.
  • extList is a cell array of character vectors that contain the user's input list
  • nExt is the number of extensions entered
ext = inputdlg('Please enter file extensions separated by a comma: **\*. Example: txt, csv, ogg');
if ~isempty(ext) %check if user entered anything
% Separate selections
extList = strtrim(strsplit(ext{1},','));
extList(cellfun(@isempty,extList)) = []; % get rid of empties (if any)
nExt = numel(extList); %number of extensions
For example, in the inputdlg enter this: txt, csv, ogg, xlsx
If there is a set list of possible file extensions, a better idea would be to use a listbox where the user can merely select from a set of options.
Adam Danz
Adam Danz on 13 Sep 2019
Edited: Adam Danz on 13 Sep 2019
Hmmm, could you show us exactly what's in ext and the result of class(ext)?
I just embedded the code from my answer into app designer and entered a variety of inputs including several extensions, 1 extension, and empty, and there was no error.

Sign in to comment.

More Answers (2)

Nom on 13 Sep 2019
Right before
extList = strtrim(strsplit(ext{1},','));
is executed.
K>> ext
ext =
'txt, pdf, docx'
K>> class(ext)
ans =
Above what I put into the command script
Adam Danz
Adam Danz on 13 Sep 2019
inputdlg should always return a cell array:
Where is the 'txt, pdf, docx' char array coming from? I can't be the output to inputdlg.

Sign in to comment.

Nom on 13 Sep 2019
I'm forever in your debt
Works perfectly!
Nom on 17 Sep 2019
Oh wow, I completely forgot about that.
Thank you so much

Sign in to comment.


Find more on File Operations 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!