フィルターのクリア

uigetfile Multiselect option not working for single file select

11 ビュー (過去 30 日間)
Michael Shane
Michael Shane 2024 年 4 月 16 日
コメント済み: Voss 2024 年 4 月 22 日
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 件のコメント
Michael Shane
Michael Shane 2024 年 4 月 16 日
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 2024 年 4 月 20 日
"I would just have to have two different scripts. One for a single file, one for multiple.2
Not required. Just use CELLSTR.

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 4 月 19 日
移動済み: Voss 2024 年 4 月 20 日
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 件のコメント
Michael Shane
Michael Shane 2024 年 4 月 22 日
Wow, that worked perfectly. I didn't even think about there being a difference in the variable types. I really appreciate it.
Voss
Voss 2024 年 4 月 22 日
You're welcome!

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2024 年 4 月 20 日
[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 2024 年 4 月 19 日
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.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by