Effacer les filtres
Effacer les filtres

Help with set(handles.text) for a GUI

41 vues (au cours des 30 derniers jours)
IKER ARRIEN
IKER ARRIEN le 23 Nov 2017
Commenté : IKER ARRIEN le 23 Nov 2017
I am creating a GUI where there are several "boxes" that should be filled in with numerical values by the user.
At one point, I would like to change all the values of those boxes to 5 (for example) and that the user can see the 5 inside those boxes.
As there are many boxes, I would like to do it automatically with a "for" loop.
This is how I am trying to do it:
%%%Example of the code %%%
List = {'text1', 'text2', 'text3'};
for index = 1:length(List)
set(handles.List{index},'String',5);
end
%%%End of example %%%
However, Matlab returns the following error message:
Reference to non-existent field 'List'.
I understand that this is because the names of the boxes are "text1", "text2" and "text3" and not "List".
However, if I write "List{1}" on the command window, it gives back text1 (and if I put List(1) it gives back 'text1').
Therefore, I was hoping that set(handle) would recognise the value of each element of List (text1, text2 and text3) instead of List itself.
I am trying to do it that way in order to avoid doing it in the following way:
%%%Coding I want to avoid %%%
set(handles.text1,'String',5);
set(handles.text2,'String',5);
set(handles.text3,'String',5);
%%%End %%%
Am I missing something?
Is there another way to achieve what I am trying to do?
Thank you in advance.

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Nov 2017
set(handles.(List{index}),'String',5);
  1 commentaire
IKER ARRIEN
IKER ARRIEN le 23 Nov 2017
Thank you Walter

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 23 Nov 2017
Modifié(e) : Jan le 23 Nov 2017
You are almost there:
List = {'text1', 'text2', 'text3'};
for index = 1:length(List)
set(handles.(List{index}), 'String', '5');
% ^ ^
end
Only the marked parentheses have been missing to use "dynamic fieldnames".
Alternatively:
handles.List = [handles.text1, handles.text2, handles.text3];
set(handles.List, 'String', '5');
If you have different strings, you can use a cell string in column shape:
set(handles.List, {'String'}, {'5'; '6'; '7'});
Then the property must be a cell string also.
  1 commentaire
IKER ARRIEN
IKER ARRIEN le 23 Nov 2017
Thank you Jan.
I am sorry that I cannot accept more than one answer.
You have my upvote though.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by