How to get data from a variable number of Edit Text box in Matlab GUI ?

44 views (last 30 days)
Hello everyone,
I am creating a GUI where a variable number of edit box is created based on the number entered from the user. Then I need to get the data written in each edit box. To do that I created a cycle for where N edit text boxes are generated using uicontrol.
for K=1:N
edit(K) = uicontrol('Style', 'edit', 'String', '', 'Units', 'Pixels', 'Position', [175 ypos 110 30],'callback',@thickness);
The problem is that I cannot get the values from each box because it gives me only the value written in the last box.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function []=thickness(hObject, eventdata, handles)
setappdata(0, 'value',val);
Do you have any suggestion how to save the data from all the boxes for example in a array? Thank you in advance

Accepted Answer

Stephen23 on 22 May 2018
Edited: Stephen23 on 22 May 2018
You will need to pass the array of handles edit to the callback/function where you want to access them all, and either:
  • loop over the handles just like when creating the edit boxes, or
  • simply get all values at once: get(edit,'String')
What you are doing now will only access one edit box, because when the callback thickness is called the input hObject only contains the handle for that one edit box that triggered the callback, not for all of the handles. Thus you need to pass all the handles yourself if you want to access them all at once.
MDC on 23 May 2018
You are completely right! Now everything works good! Thank you so much!

Sign in to comment.

More Answers (1)

Ahmed Anas
Ahmed Anas on 3 Aug 2019
Kindly send me the complete code for this, cant understand it well and have following error.
Error using My>thickness (line 135)
Not enough input arguments.
Error while evaluating UIControl Callback
Error using My>thickness (line 135)
Not enough input arguments.
Error while evaluating UIControl Callback


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!