Help with set(handles.text) for a GUI

29 ビュー (過去 30 日間)
IKER ARRIEN
IKER ARRIEN 2017 年 11 月 23 日
コメント済み: IKER ARRIEN 2017 年 11 月 23 日
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 件のコメント
IKER ARRIEN
IKER ARRIEN 2017 年 11 月 23 日
Thank you Walter

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by