MATLAB: How to reference a property name with a variable in findobj

findobjhandle

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.

Best Answer

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}