Art
Art

Reputation: 1335

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) :/

Upvotes: 3

Views: 13666

Answers (2)

cooltea
cooltea

Reputation: 1113

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.

Upvotes: 5

Peter
Peter

Reputation: 11

filename = cellstr(filename)

is another (easier) solution to always receive a cell of filenames indpendent if 1 or more files are choosen

Upvotes: 1

Related Questions