Help with set(handles.text) for a GUI
29 ビュー (過去 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.
0 件のコメント
採用された回答
その他の回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!