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

1 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2019-9-13
编辑:Adam Danz 2019-9-13
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
end
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.
  12 个评论
Nom
Nom 2019-9-13
Thank you! Yes I've already implemented it in that exact way,
weirdly enough running this script from a .m file works flawlessly
But when I moved the new code into my app designer's code it gives this error.
Cell contents reference from a non-cell array object.
on this line
extList = strtrim(strsplit(ext{1},','));
Which is pretty confusing because this is the exact code which worked straight from a .m script file.
The app designer's code was functioning with my old way (three file extensions hard coded in)
So I'm not sure why app designer is seeing something differently
Adam Danz
Adam Danz 2019-9-13
编辑:Adam Danz 2019-9-13
Hmmm, could you show us exactly what's in ext and the result of class(ext)?
[update]
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.

请先登录,再进行评论。

更多回答(2 个)

Nom
Nom 2019-9-13
Right before
extList = strtrim(strsplit(ext{1},','));
is executed.
K>> ext
ext =
'txt, pdf, docx'
K>> class(ext)
ans =
'char'
Above what I put into the command script
  2 个评论
Walter Roberson
Walter Roberson 2019-9-13
So somehow ext is a character vector instead of a cell array of char.
As a general fix: after you have called
ext = inputdlg('Please enter file extensions separated by a comma:. Example: txt, csv, ogg: ');
add
ext = cellstr(ext);
Though you might have to check isempty() first.
Adam Danz
Adam Danz 2019-9-13
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.

请先登录,再进行评论。


Nom
Nom 2019-9-13
Awesome!
I'm forever in your debt
Works perfectly!
  17 个评论
Adam Danz
Adam Danz 2019-9-17
I'm not sure how that solved the error you were getting.
Notice at the end of this line you transpose the array.
fullPaths = strrep(fullPaths,fileLoc,'')'; % Remove the header path
% HERE ^
So when you comment that out, fullPaths remains as a row-cell-array.
That causes the error when you try to horizontally concatenate the bottom line below.
outputData = [{'Starting Path:' fileLoc ' ';
'Time to Execute:' [sprintf('%.2f',overallTime+overallTime2) 's'] ' ';
'File Count:' num2str(sum(finalFlag)) ' ';
'Date' 'Size (kb)' 'File'};
fileDates(finalFlag) num2cell(fileSizes(finalFlag) ./1024) fullPaths(finalFlag)];
%|___column array___| {______________Column array_________| |____Row array______|

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by