Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Rachel Anthony
Rachel Anthony le 11 Juil 2018
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.

Réponse acceptée

Steven Lord
Steven Lord le 11 Juil 2018
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}

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by