Search code examples
matlabfile

matlab: uigetfile with one or multiple files


I have a stupid problem. I want to know how many files were selected after:

[fileName, pathName, filterIndex] = uigetfile({'*.*';'*.xls';'*.txt';'*.csv'}, 'Select file(s)', 'MultiSelect', 'on');

when there was more than 1, i can do length(fileName);

and its ok. But when there was only one selected, this gives me actual length of fileName (amount of chars) :/


Solution

  • You should probably check with iscell(filename) first.

    [fileName, pathName, filterIndex] = uigetfile({'*.*';'*.xls';'*.txt';'*.csv'}, 'Select file(s)', 'MultiSelect', 'on');
    
    if isequal(fileName, 0)
        disp('User selected Cancel');
    else
        if iscell(fileName)
            nbfiles = length(fileName);
        elseif ~isempty(fileName)
            nbfiles = 1;
        else
            nbfiles = 0;
        end
    end
    

    UPDATE: added check on cancellation from uigetfile as was suggested in a comment.