Dynamically add edit boxes using uicontrol and how to get the reference

Asked by Karthik KJ on 23 Apr 2012
Latest activity Commented on by Karthik KJ on 24 Apr 2012

Hi I am new to Matlab GUI, I would like to add edit boxes using uicontrol. Since the edit boxes are created during runtime, I could not use string property to get the values entered by the user.

The code I started with is

function pushbutton1_Callback(hObject, eventdata, handles) for i=1:10 uicontrol('Style', 'edit', 'Position', [150, 5 + (i-1)*25, 60, 22],... 'Callback', {@myCallback, i}); end

This will create 10 edit boxes when i click pushbutton1, now when i run the scrip. I will have 10 edit boxes.What i need here is, user will enter 10 entries into the editboxes and I need to save these 10 entries to the workspace when i click save pushbutton. How can i get this, I tried to get the handles, (text_handles(i)=gco) but no success.Please help me on this.

0 Comments

Karthik KJ

Products

No products are associated with this question.

2 Answers

Answer by Titus Edelhofer on 23 Apr 2012
Accepted answer

Hi,

  • add the handle from the uicontrol to the handles structure:
handles.edit = zeros(1, 10);
handles.edit(i) = uicontrol('style', 'edit', ...);
guidata(hObject, handles)
  • in the save callback you collect the entries:
entries = cell(1, 10);
for i=1:10
  entries{i} = get(handles.edit(i), 'string');
end

Titus

3 Comments

Karthik KJ on 23 Apr 2012

Hi i done like this as you mentioned
function pushbutton1_Callback(hObject, eventdata, handles)
for i=1:str2num(get(handles.entryNos,'string'))
handles.edit = zeros(1, str2num(get(handles.entryNos,'string')));
handles.edit(i) = uicontrol('style', 'edit','Position', [10, 5 + (i-1)*25, 60, 22]);
guidata(hObject, handles)
end

function pushbutton3_Callback(hObject, eventdata, handles)
entries = cell(1, str2num(get(handles.entryNos,'string')));
for i=1:str2num(get(handles.entryNos,'string'))
entries{i} = get(handles.edit(i), 'string');
end

I am getting this error,
Error using ==> testuicontrol1('pushbutton3_Callback',gcbo,[],guidata(gcbo))
There is no 'string' property in the 'root' class.

Daniel on 23 Apr 2012

Put this line

handles.edit = zeros(1, str2num(get(handles.entryNos,'string')));

outside the for loop.

Karthik KJ on 24 Apr 2012

Hi I am creating the edit boxes as you mentioned and Now if the edit boxes reaches the limit of the gui figure then i would like to use the slider bar.So that I can have any number of edit boxes, which user can enter the inputs. How to incorporate slider with the creation of the edit boxes. Please help on this.

Titus Edelhofer
Answer by Daniel on 23 Apr 2012

You need to save the handles that are created by uicontrol into the gui handles object ...

    handles.hEdit(i) = uicontrol( ...

Then you need to save the updated handles with setappdata. Then the callback for the save button should be able to get the handles for the edit boxes.

1 Comment

Karthik KJ on 23 Apr 2012

Thank you Daniel

Daniel

Contact us