필터 지우기
필터 지우기

How to reference a property name with a variable in findobj?

조회 수: 3 (최근 30일)
Rachel Anthony
Rachel Anthony 2018년 7월 11일
답변: Steven Lord 2018년 7월 11일
In my code, I am looping through several property names in order to produce different handles. In my case, I have an array called list, which stores different group names.
list = ['G1', 'G2', 'G3']
I have 8 different objects which all have one of these group names. I am using the following code to find a handle for objects with specific group names. I am using a loop to produce these handles.
for x = 1:numel(list)
grp = findobj(obj, 'GrpName', list(x))
% other code that is not important for this problem
end
Theoretically, since list(1) = 'G1', during the first loop grp should yield a handle with the objects that have the GrpName 'G1' (when I hardcode the value 'G1' into the findobj function it correctly produces a handle). However, grp is empty presumably because of the variable name. Am I formatting this incorrectly? Thanks in advance.

채택된 답변

Steven Lord
Steven Lord 2018년 7월 11일
Look at the contents of your list variable. It is not a 3 element array, the first element of which is 'G1'.
You likely want to create list as a cell array rather than a char array. Note to extract the char array stored in each element of the cell array, you need to use curly braces to index into the cell array. Using parentheses would extract a smaller cell array.
list = {'G1', 'G2', 'G3'};
list{1}

추가 답변 (0개)

카테고리

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