Why does uigetfile return a different data type for filename depending upon the number of files selected?

12 views (last 30 days)
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.

Accepted Answer

dpb
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
Gwendolyn Shaw
Gwendolyn Shaw on 14 Jul 2015
Edited: Gwendolyn Shaw on 14 Jul 2015
Thank you dpb.
The if isstr... works.
While if files==0...gives up the appropriate error when no files are selected and runs smoothly if one file is selected, it gives the following error when two files are selected:
Undefined function 'eq' for input arguments of type 'cell'.
Error in Program Name (line X) if files==0, error('No files selected'),end;
I did look at help for so long that I forgot what I was trying to accomplish.
dpb
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...

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!