I created an array of objects in app designer. They're all images.
images=[app.Image1,app.Image2,.......,app.ImageX];
now I get their visible property
check=get(images,'Visible');
that gives me a cell array of 'on' and 'off' values. I want to check if all the values in the array are 'on'. How do I do that? I tried combining it into a matrix, but got errors.

 채택된 답변

DGM
DGM 2021년 4월 26일
편집: DGM 2021년 4월 26일

0 개 추천

Consider the example
A = {'on'};
A = repmat(A,[10 1])
% A{2} = 'off' % uncomment to test the response to the existence of an 'off' instance
f = @(x) strcmp(x,'on');
allon = all(cellfun(f,A)) % returns scalar logical
EDIT:
Actually, Image Analyst has a point. The regular string comparison tools would be simpler (about 50x faster too) and don't even need cellfun()
allon = all(strcmp(A,'on'))
I don't know why I chose to use cellfun() for that. I just had it on my mind at the time and ran with it.

추가 답변 (2개)

Image Analyst
Image Analyst 2021년 4월 26일

0 개 추천

Try this:
>> ca = {'on', 'on', 'on'}
ca =
1×3 cell array
{'on'} {'on'} {'on'}
>> allOn = all(ismember(ca, {'on'}))
allOn =
logical
1
>> ca = {'on', 'on', 'off'}
ca =
1×3 cell array
{'on'} {'on'} {'off'}
>> allOn = all(ismember(ca, {'on'}))
allOn =
logical
0

댓글 수: 3

Vojtech Siler
Vojtech Siler 2021년 4월 26일
This is giving me this error:
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of character vectors, unless one is a character vector.
Image Analyst
Image Analyst 2021년 4월 27일
You changed something. My code works. I don't have 34 lines in my code. I had only 2 lines and they worked. You don't need anonymous functions, strcmp(), cellfun(), etc. All you need is the single line of code I gave you that calls ismember().
Vojtech Siler
Vojtech Siler 2021년 4월 27일
I inserted this code into my code and changed the name of the array to what I had.

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

Steven Lord
Steven Lord 2021년 4월 26일

0 개 추천

One easy way to do this is to use a string.
A = {'on', 'on', 'on'};
all(A == "on")
ans = logical
1

댓글 수: 2

Vojtech Siler
Vojtech Siler 2021년 4월 26일
I think I remember trying that, because I found a similar thing as an answer to a similar problem, but I think it threw errors. I'll try this specifically tomorrow, though, and see if it works. Thanks for the answer!
DGM
DGM 2021년 4월 27일
It depends what version you have. This usage with the double quoted string needs R2016b to work (IIRC). At least it doesn't work for me in R2015b.

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

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2021년 4월 26일

댓글:

2021년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by