필터 지우기
필터 지우기

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.

댓글을 달려면 로그인하십시오.

추가 답변 (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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by