필터 지우기
필터 지우기

Help with set(handles.text) for a GUI

조회 수: 41 (최근 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개)

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에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by