Combine numeric and character array to display to operator

2 views (last 30 days)
I have a script which uses a function to search a folder for files with that match inputs passed to the search function.
The output of the search function is a cell array which displays all of the potential matches. Ex:
searchres = filefinder(searchdirectory,'searchterm1','searchterm2')
The result of this function is the following:
searchres = 1×3 cell array
{'file1.xlsx'} {'file2.xlsx'} {'file3.xlsx'}
What I am attempting to do is to query the operator to select the single correct file to operate on via an input prompt.
prompt=input('Type the correct .xlsx file number (1,2,3...) to perform data reduction \n');
disp(searchres')
The user would type "1" to select the first file, "2" for the second, and so on until the list is complete. However, with more files present, I'd rather not have the user count 15-20 lines to figure out what number to type. I'd like for it to be displayed as follows:
1 'file1.xlsx'
2 'file2.xlsx'
3 'file3.xlsx'
...
n 'filen.xlsx'
I've tried using a loop to put the two together (column of numbers, column of text), but it doesn't work.
searchlen=length(searchres);
for i=1:searchlen
searchval=cell2mat(searchres(i))
searchselect(2,i)=searchres(i);
searchselect(1,i)=i;
end
But I get the error "Conversion to double from cell is not possible."
Am I going about this in the wrong way? I'm certian this is something that I can do.

Accepted Answer

Steven Lord
Steven Lord on 18 Jul 2019
Edited: Steven Lord on 18 Jul 2019
Using a listdlg as Star Strider suggested would be my first thought. But if the files all have the exact same naming pattern ("file" followed by a number followed by ".xlsx") you don't need to search. Just build the name from the number the user enters.
n = 42;
theFileChar1 = sprintf('file%d.xlsx', n)
theFileChar2 = ['file' num2str(n) '.xlsx']
theFileString = "file" + n + ".xlsx"
If for some reason you must display the list of names and their associated numbers as text, I'd use a table. Use curly braces to extract the selected file.
thefiles = {'file1.xlsx', 'file2.xlsx', 'file3.xlsx'};
filenum = 1:numel(thefiles);
filesByNumber = table(filenum.', thefiles.', ...
'VariableNames', {'Number', 'FileName'})
selectedNumber = input("Select a file by number " + ...
"from the table displayed above: ");
selectedFile = filesByNumber{selectedNumber, 'FileName'}
  2 Comments
Zachary Reed
Zachary Reed on 18 Jul 2019
I think listdlg will work very well for my purposes.
However, the file the user needs to select (from a list of many files) all follow a very similar naming convention, but differentiate by some key data within the file name. That data is present as part of the function already, so I was using that to help the system autmatically narrow in on the potential matches.
Ex:
User has a 264 caliber 140 grain projectile work is being perfomed on. "264" and "140" are variables present within the function, so using those the search would narrow in on two files in the following list:
  • 243cal105grBergerHybrid.xlsx
  • 308cal168grSierraMatchKing.xlsx
  • 264cal140grBergerHybridTarget.xlsx
  • 264cal140grHornadyBTHP.xlsx
The user would then be presented with the option of selecting either projectile, depending on exactly which one it is. I'd like to automate the whole thing, but sometimes due to variances in brand naming I think a small amount of human input is necessary.
  • 264cal140grBergerHybridTarget.xlsx
  • 264cal140grHornadyBTHP.xlsx
I tried using listdlg with my 1x3 cell array as an input, but received the following error: "Arguments to LISTDLG must be param/value pairs."

Sign in to comment.

More Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!