GUI Push Button Undefined Variable

I am attempting to create a GUI with a push button that solves some equations for me. The data that it is using is gained from some edit text boxes. This is how the data is being read in:
function edit2_Callback(hObject, eventdata, handles)
r = str2double(get(handles.edit2,'string'));
assignin('base','r',r)
In my workspace, the variable r shows up as the value that was inputted. In the push button callback I am attempting to access that variable and it shows up with an error of "Reference to non-existent field 'r'".
This is what my code for the push button looks like:
function pushbutton1_Callback(hObject, eventdata, handles)
%%Find Chi
r1 = 2*handles.r+2;
r2 = 2*handles.r;
I have tried removing the handles from this expression as well and that gave me the same error.

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 2월 17일

0 개 추천

Sara - the handles structure does not reference those variables that you save to the workspace. It only includes the handles to the GUI controls and any user-defined data that you have saved to the structure using guidata. As such, you can access your edit2 control directly through the handles object. Remove the edit2_Callback function, and within the push button callback do
function pushbutton1_Callback(hObject, eventdata, handles)
r = str2double(char(get(handles.edit2,'String')));
%%Find Chi
r1 = 2*r+2;
r2 = 2*r;
% etc.
Try the above and see what happens!
As an aside, in your edit2_Callback you do the following
r = str2double(get(handles.edit2,'string'));
In this case, you don't need to access handles as hObject is handles.edit2.

댓글 수: 2

Sara Koniecko
Sara Koniecko 2016년 2월 18일
I have 3 edit text boxes that I need to do this for, can I use this method for all of them and just replace the tag that is associated with handles?
Yes, Sara. Just do the same as above, using the tag of the control as a field within handles. For example,
get(handles.edit1,'String');
get(handles.edit2,'String');
etc.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming Utilities에 대해 자세히 알아보기

질문:

2016년 2월 17일

편집:

2016년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by