필터 지우기
필터 지우기

How to check if one file selected when using MultiSelection in matlab?

조회 수: 20 (최근 30일)
I have a function that allow user to browse and choose some images like this
[filename, pathname, filterindex] = uigetfile( ...
{ '*.jpg;*.jpeg;*.png','Images (*.jpg, *.jpeg, *.png)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose Class 1 DataSet', ...
'MultiSelect', 'on');
And I take the browsed images in a cell array
ChosenImages = strcat(pathname,filename);
Now I have a problem, if the user entered one image only, it doesn't return this image as one cell, it return each character as cell, and when I use
length(ChosenImages);
it returns the number of chars in the link!
also I tried
ischar(ChosenImages{1})
to check if one cell is a cell or not but it gives me an error too.
What can I do to check if the user chosen one image or multiple images?

채택된 답변

Guillaume
Guillaume 2015년 12월 4일
<rant> This is a thing I hate about the way mathworks design their language. All these special cases where a function behaves differently (when n = 1 or when n is vector, or...). It hurts genericity and force you to write 'ifs' all over the place when you're expecting to cope with both the general case and the exception. And it's doubly frustrating as they must have the same if pattern in the function you call to generate that exceptional case in the first place <end rant>
To fix the problem, simply convert the exceptional case back to the generic case with:
[filename, pathname, filterindex] = uigetfile( ...
{ '*.jpg;*.jpeg;*.png','Images (*.jpg, *.jpeg, *.png)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose Class 1 DataSet', ...
'MultiSelect', 'on');
if ~iscell(filename)
filename = {filename};
end %now filename is a cell array regardless of the number of selected files.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 4일
[filename, pathname, filterindex] = uigetfile( ...
{ '*.jpg;*.jpeg;*.png','Images (*.jpg, *.jpeg, *.png)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose Class 1 DataSet', ...
'MultiSelect', 'on');
filename = cellstr(filename);
%now filename is a cell array regardless of the number of selected files.
  댓글 수: 2
Eslam Hamed
Eslam Hamed 2015년 12월 4일
Your Answer is brilliant, thank you a lot it works ^_^
Ahmed Harun-Al-Rashid
Ahmed Harun-Al-Rashid 2016년 11월 4일
Dear Roberson, I also get help to solve similar problem using your answer. Thank you so much.
Sincerely, Rashid

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by