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.
uigetfile Multiselect option not working for single file select
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Michael Shane
am 16 Apr. 2024
Kommentiert: Voss
am 22 Apr. 2024
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 Kommentare
Stephen23
am 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.
Akzeptierte Antwort
Voss
am 19 Apr. 2024
Verschoben: Voss
am 20 Apr. 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 Kommentare
Weitere Antworten (2)
Walter Roberson
am 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)
0 Kommentare
Dallas Perkins
am 19 Apr. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!