Combine numeric and character array to display to operator
2 views (last 30 days)
Show older comments
Zachary Reed
on 18 Jul 2019
Commented: Zachary Reed
on 18 Jul 2019
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.
0 Comments
Accepted Answer
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'}
More Answers (0)
See Also
Categories
Find more on Dialog Boxes 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!