Multiple Edit boxes and PPmenus

조회 수: 7 (최근 30일)
Santosh
Santosh 2013년 4월 25일
I am using GUIDE to build a MATLAB GUI.
I have 24 edit text boxes on the GUI.
How can I use loop in the code (like: for i = 1 to 24) to retrieve and store the contents of these components in to a 24 by 1 array.
Have I had less edit text boxes, I could manually name each box as textbox1, textbox2 and retrieve the contents of it using get(handles.textbox1,'String') but I have so many text boxes. I want to avoid manually naming them as textbox1 .....to textbox24 and then retrieve data from each of the text boxes.
Is there any easy way to do this.
Appreciate the help
Thanks, Santosh

채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 25일
boxvals = cell(24,1);
for i = 1 : length(boxvals)
boxvals{i} = get( handles.(sprintf('textbox%d', K)), 'String' );
end
If you were not using GUIDE, or are willing to add in non-GUIDE code to create the boxes, then like I showed in response to your last question, create them in a loop and store the handles.
nbox = 24;
editboxes = zeros(nbox,1);
for K = 1 : nbox
editboxes(K) = uicontrol( 'Style', 'edit', 'Units', 'norm', 'Position', [1/2 (nbox-K)/nbox 1/2 1/(nbox+1)], 'String', {sprintf('edit box #%d', K)} );
end
then to fetch,
boxvals = cell(nbox, 1);
for K = 1 : nbox
boxvals{i} = get( editboxes(K), 'String' );
end
  댓글 수: 1
Santosh
Santosh 2013년 4월 26일
Walter,
Thanks for the advice, this works great.
I think, I have spent too much time on how to program with GUIDE, so I am just going to stick with it instead of doing the programmatic gui development. However, I am paying attention to the code that you posted for programmatic gui development and it makes a lot of sense to me
Really Aprreciate this, Santosh

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by