Vector loop over multiple object handles inside a set function

조회 수: 2 (최근 30일)
Joshua
Joshua 2019년 3월 6일
댓글: Joshua 2019년 3월 19일
Hi,
following problem: I have an array of struct elements with each containing a handle element (surface object in my case) for which I want to set the Visible property to an equivalent cell array containing 'on' and 'off'.
Here is how I would imagine the code line to look similar with:
set([structWithStuff(:).surfaceHandle], 'Visible', cellArrayWithOnsAndOffs(:));
The code above obviously is faulty. Assuming, this is even possible within MATLAB, what would be an appropriate way to make this work? Or would the more efficient way actually be a simple for loop?

채택된 답변

Steven Lord
Steven Lord 2019년 3월 6일
No for loop is required. See the "Set Line Style to Different Value for Multiple Lines" and "Set Different Values for Multiple Properties on Multiple Objects" examples on the documentation page for the set function. The first input should be a vector of handles, the second the cell array {'Visible'}, and the third the cell array of values to which the Visible property of the objects in the vector of handles should be set.
In the first of the examples on the documentation page P is a vector of 4 handles, NameArray is a 1 element cell array, and ValueArray is a 4 element cell array.
In the second documentation example S is 3 handles, NameArray has 2 elements, and ValueArray is 3-by-2.
  댓글 수: 1
Joshua
Joshua 2019년 3월 19일
Sorry for the late answer. I ended up going with a different solution first due to a change in the requirements, but eventually faced the same "problem" again. Your solution worked fine for this. Thanks a lot!

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

추가 답변 (1개)

Dennis
Dennis 2019년 3월 6일
You can use logical indexing. I wrote this earlier today for another question, but it is quite close to what you want to do:
for i=3:-1:1 %creating some graphical elements
h.pb(i)=uicontrol('style','pushbutton','position',[50 50+(40*i) 80 40],'String',sprintf('Button %d',i));
end
set(h.pb(:),'callback',{@MyCallback,h});
function MyCallback(hObj,~,h)
s=[1 0 1]; % matrix with 'on' and 'off', i used 1 and 0, but you can compare strings if you prefer
set(h.pb(s==1),'enable','off'); %set all s==1 to 'off'
pause(3)
set(h.pb(:),'enable','on');
end

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by