uigetfile Multiselect option not working for single file select
Mostra commenti meno recenti
I have been trying to figure out why you can't select a single file, if you have the 'MultiSelect' option set to 'on' in the uigetfile command. I need to be able to select 'one or more' files. Here is the code:
% Select and Open file
[files, pathname] = uigetfile({'*.xls*'; '*.csv'}, ...
'Select One or More Files', 'MultiSelect', 'on');
for filecount = 1:length(files)
filename = convertCharsToStrings(files(filecount));
opts = detectImportOptions(filename, VariableNamingRule="modify");
MDRdata = readtable(filename, opts);
time = datetime(MDRdata.ZuluDate_Time,"InputFormat",'yyyy-MM-dd''T''HH:mm:ss.SSS''Z');
time.Format = 'HH:mm:ss.SSS';
....
I keep getting the following:
"Error using detectImportOptions
Unable to find or open '1'. Check the path and filename or file permissions.
Error in ApacheMDRProgram_04162024 (line 12)
opts = detectImportOptions(filename, VariableNamingRule="modify");"
It works fine if you select more than one file.
2 Commenti
Michael Shane
il 16 Apr 2024
Stephen23
il 20 Apr 2024
"I would just have to have two different scripts. One for a single file, one for multiple.2
Not required. Just use CELLSTR.
Risposta accettata
Più risposte (2)
Walter Roberson
il 20 Apr 2024
[files, pathname] = uigetfile({'*.xls*'; '*.csv'}, ...
'Select One or More Files', 'MultiSelect', 'on');
if isnumeric(files)
return; %user canceled
end
files = cellstr(files);
if files was returned as a cell array because the user selected multiple files, then cellstr() returns the cell unchanged.
If files was returned as a character vector because the user selected one file, then cellstr() wraps the character vector in a cell.
Afterwards, the result will be a cell array (that might possibly have only one entry)
Dallas Perkins
il 19 Apr 2024
0 voti
Hi Michael,
If you only select one file uigetfile will return a char array instead of a cell array of char arrays. The line calling convertCharsToStrings in that scenario will take the first char of the filename and convert it to a string instead of the full filename. This errors later since it's not a valid filename. It looks like you need to add some additional logic to detect if you are returning a single or multiple files.
Categorie
Scopri di più su Startup and Shutdown in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!