How to read data/number from dynamically added text boxes by 'for' loop using GUI ?

I have created a GUI which dynamically (during the run time) adds text boxes on the interface as per the requirement of the user by using 'for' loop. Now, I am interested in writing the values (user entered) from the dynamically added text boxes to Excel sheet by using 'for' loop. Problem that I am facing is that I am not able to retrieve the data from dynamically added text boxes by using loop..as they don't have tag names to refer. What shall I do to retrieve the data from dynamically added text boxes?
Thanks in Advance.

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 26 Mär. 2017
Vysser - if you are dynamically creating edit text boxes, then why not maintain a list of all the handles to these text boxes? If you have the handle to the control, then you can get its String property...which is the value entered by the user.

5 Kommentare

I have used the following 'for' loop and uicontrol for adding text boxes in GUI.
n=6
for i=1:n
handles.hEdit(i)=uicontrol('Style', 'edit', 'Position', [500, 50 + (i-1)*100, 101, 51]);
end
How shall I 'get' values ? Please suggest me the syntax.
Thanks in Advance.
Vysser - I think the following would work
for i=1:n
myString = char(get(handles.hEdit(i),'String'));
% write string to excel file
end
The char may not be necessary (and you may want to do a conversion from a string to a double/number).
I did the following (as recommended by you)::
for i=1:n;
Values(i)=str2double(get(handles.hEdit(i),'string'));
% Function to write data in Excel
end
But it is giving me the following error::
Reference to non-existent field 'hEdit'.
Error in ==> MARVEL>pushbutton5_Callback at 154 Values(i)=str2double(get(handles.hEdit(i),'string'));
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> MARVEL at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)MARVEL('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
What shall I do to resolve this error?
Thanks in Advance.
When you update the handles structure like you have done with
handles.hEdit(i)=uicontrol('Style', 'edit', '....
then you need to remember that you are only updating a local copy of handles. If you want to save the updated structure so that it is available to other callbacks, then you need to call guidata as
for i=1:n
handles.hEdit(i)=uicontrol('Style', 'edit', ...
'Position', [500, 50 + (i-1)*100, 101, 51]);
end
guidata(hObject,handles);
I'm assuming that there is an hObject. If the above doesn't work, then you will need to show more of your code.
It worked !! Thank you Geoff Hayes.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 25 Mär. 2017

Kommentiert:

am 27 Mär. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by