Why does uigetfile return a different data type for filename depending upon the number of files selected?
12 views (last 30 days)
Show older comments
I simply want to determine the number of files that were selected with the uigetfile function.
size(filename) doesn't work.
The size function returns 1 2 when there are two files and 1 129 when there is a single file. I assume that uigetfile returns an array of strings when there are multiple files and a cell array when there is just one file selected.
Why does MATLAB do this, and how do I fix it?
I have tried variations of iscell, iscellstr, ischar to determine what data type of the single-file filename.
I have tried various ways of concatenation including sprintf,char,str, ...
Related complaint: The MATLAB documentation doesn't list uigetfile under Data and File Management. You have to hunt it down or already know what it is called.
1 Comment
Jurgen
on 4 Jan 2018
I think in modern versions you can cast the output into the string datatype to get the desired behavior.
Accepted Answer
dpb
on 14 Jul 2015
Edited: dpb
on 16 Jul 2015
Why? "Because" is the only answer that matters. It is documented behavior.
While it is something of a pit(proverbial)a(ppendage) I agree, it's not too tough to deal with. I generally write sotoo when using 'MultiSelect','on' --
[files,path,...]=uigetfile('*.m','multiselect','on');
if isfloat(files), error('No files selected'),end
% Clean up return string if only one file; figment from 'multiselect'
if isstr(files), files={files}; end % convert char string to cellstr
for n = 1:length(files)
...
Simply converting in case there is only one before doing anything else means you always are dealing after that point with a cell array and so length functions as expected.
I agree they ( ui[get|put]file) should be at least cross-referenced under file I/O functions; however, since it's a GUI-based control TMW has placed them under the GUI-related stuff instead.
The old standby help is good for finding stuff like this if you aren't sure--start with it alone then look at the subject areas that seem promising. There's also lookfor that searches the comment content for keywords that can locate things if can get a reasonable key word.
Name explosion is a real issue w/ MATLAB however, granted...
2 Comments
dpb
on 14 Jul 2015
Oh, yeah, I just did that from memory and forgot that what you've really got to check for is whether the return is numeric, not the value unless you also check for the cellstr/char dichotomy there as well. Replace the ==0 with isfloat(). I made the correction plus fixed an inadvertent change in variable name between the uigetfile call and the later lines in the Answer...
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!