MATLAB: Multiple Edit boxes and PPmenus

matlab gui

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

Best Answer

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