What caused GUI problem after Push Button is pressed and any recommended solution?

1 view (last 30 days)
Hi. I have a GUI problem.
Here is code for the Push Button.
function runButton_Callback(hObject, eventdata, handles)
% selects graph1 as the current axes, so that
% Matlab knows where to plot the data
axes(handles.graph1)
range_find(pt, tau, fr, time_ti, gt, gr, freq, ...
sigma, te, nf, loss, snro, range, out_option);
guidata(hObject, handles); % updates the handles
This is the code for the edit text for Pt
function ptEdit_Callback(hObject, eventdata, handles)
pt = str2num(get(hObject,'String'));
% checks to see if input is empty. if so, default input1_editText to 100
if (isempty(pt))
set(hObject,'String','100')
pt = str2num(get(hObject,'String'));
msgbox('Please enter number only')
end
guidata(hObject, handles);
function ptEdit_CreateFcn(hObject, eventdata, handles)
% 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
When I run the GUI, I have the following error message ??? Undefined function or variable 'pt'.
Error in ==> mark_v1>runButton_Callback at 86 range_find(pt, tau, fr, time_ti, gt, gr, freq, ...
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> mark_v1 at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)mark_v1('runButton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
What can I do to overcome this problem? Any advice regarding this matter is highly appreciated. Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Dec 2011
  4 Comments
Eric
Eric on 20 Dec 2011
The function below is where the pt function is defined:
function runButton_Callback(hObject, eventdata, handles)
...
handles.pt = pt;
range_find(handles.pt, ...);
guidata(hObject, handles); % updates the handles
At which function callback should I declare pt variable?
Is it at
function ptEdit_Callback(hObject, eventdata, handles)
handles.pt = str2num(get(hObject,'String'));
...
guidata(hObject, handles);
or
function mark_v1_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for mark_v1
handles.pt;
guidata(hObject, handles);
Thanks
Walter Roberson
Walter Roberson on 20 Dec 2011
If you already assign to handles.pt in ptEdit_Callback then you do not need to assign pt to handles.pt in runButton_Callback -- but you do need to ensure that runButton_Callback is not called until after ptEdit_Callback, such as by setting runButton to 'Enable', 'off' at the beginning of the program, and only setting it to 'Enable' 'on' after ptEdit_Callback has checked the data and verified that the user-entered pt value is valid. (You should be checking for bad data in ptEdit_Callback; at minimum you should check that the str2num did not return [] or NaN. And it is better and much more secure to use str2double() than str2num

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!