Borrar filtros
Borrar filtros

uigetfile Multiselect option not working for single file select

11 visualizaciones (últimos 30 días)
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 comentarios
Michael Shane
Michael Shane el 16 de Abr. de 2024
If the answer is this configuration only lets you select multiple files then that is fine. I would just have to have two different scripts. One for a single file, one for multiple.
Stephen23
Stephen23 el 20 de Abr. de 2024
"I would just have to have two different scripts. One for a single file, one for multiple.2
Not required. Just use CELLSTR.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 19 de Abr. de 2024
Movida: Voss el 20 de Abr. de 2024
You don't need separate scipts/functions.
Since files is a cell array when multiple files were selected, and files is a character vector when one file was selected, simply make files a cell array (the more general case) if it's not already:
if ~iscell(files)
files = {files};
end
Or, more succinctly, without explicitly checking the class yourself:
files = cellstr(files);
By the way, you don't need convertCharsToStrings (or to convert to strings at all). You can index files using curly braces {} to get the character vector representing the file name.
% Select and Open file(s)
[files, pathname] = uigetfile({'*.xls*'; '*.csv'}, ...
'Select One or More Files', 'MultiSelect', 'on');
% user hit cancel -> do nothing
if isequal(files,0)
return
end
% make sure files is a cell array
if ~iscell(files)
files = {files};
end
% loop over the selected files
for filecount = 1:length(files)
filename = files{filecount};
% ...
end
  2 comentarios
Michael Shane
Michael Shane el 22 de Abr. de 2024
Wow, that worked perfectly. I didn't even think about there being a difference in the variable types. I really appreciate it.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 20 de Abr. de 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
Dallas Perkins el 19 de Abr. de 2024
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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by