Help with set(handles.text) for a GUI

26 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2017-11-23
set(handles.(List{index}),'String',5);

更多回答(1 个)

Jan
Jan 2017-11-23
编辑:Jan 2017-11-23
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 个评论
IKER ARRIEN
IKER ARRIEN 2017-11-23
Thank you Jan.
I am sorry that I cannot accept more than one answer.
You have my upvote though.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by