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
2016-2-17
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
2016-2-18
Geoff Hayes
2016-2-18
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!