MATLAB Answers

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

72 ビュー (過去 30 日間)
MDC
MDC 2018 年 5 月 22 日
回答済み: Ahmed Anas 2019 年 8 月 3 日
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);
end
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)
val=get(hObject,'string')
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

  0 件のコメント

Sign in to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2018 年 5 月 22 日
編集済み: Stephen Cobeldick 2018 年 5 月 22 日
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.

  5 件のコメント

表示 2 件の古いコメント
MDC
MDC 2018 年 5 月 23 日
If I run this code (using get(edit, 'string'):
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
for K=1:2
ypos=300-K*50;
edit(K)= uicontrol('Style', 'edit', 'String', '', 'Units', 'Pixels', 'Position', [175 ypos 110 30],'callback',@thickness);
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
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)
val=get(edit,'value')
setappdata(0, 'value',val);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=getappdata(0,'value',val);
disp(a)
I got this error:
Error using get
Invalid handle
Error in tentativo>thickness (line 118)
val=get(edit,'value')
Error while evaluating UIControl Callback
Error using get
Invalid handle
Error in tentativo>thickness (line 118)
val=get(edit,'value')
Error while evaluating UIControl Callback
Undefined function or variable 'val'.
Error in tentativo>pushbutton3_Callback (line 129)
a=getappdata(0,'value',val);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in tentativo (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)tentativo('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Stephen Cobeldick
Stephen Cobeldick 2018 年 5 月 23 日
@MDC: you forgot to pass the handles to the callback where you want to use them, just like I told you to do in my answer. Have a look at the callback thickness: where is edit defined? Answer: nowhere, thus the error. You will need to pass edit, e.g. using the handles structure:
for k = ...
handles.edit(k) = ...
end
guidata(hObject,handles)
and in the callback where you want to use edit:
get(handles.edit,'String')
If you do not pass edit to the other callbacks, then it is not available in those other callbacks. You have to explicitly pass it in the code, because variables do not just magically jump from one callback to another (or from one workspace to another) (and nor should they).
MDC
MDC 2018 年 5 月 23 日
You are completely right! Now everything works good! Thank you so much!

Sign in to comment.

その他の回答 (1 件)

Ahmed Anas
Ahmed Anas 2019 年 8 月 3 日
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

  0 件のコメント

Sign in to comment.


Translated by