How to display an error message in gui?
Hye guyz. I need an assistance with this. I have a figure of table. The user will insert a data into the table. If the user suddenly wrong insert the data, the table will be 'NaN'. My question is how i want to make the table does not display 'NaN' on the table but I want an error message appear. I have this coding:
function Mytable1_CreateFcn(hObject, eventdata, handles)
if isnan(Mytable1)
set(hObj开发者_开发百科ect, 'Data', 0);
errordlg('Input must be a number','Error');
end
handles.Mytable2 = hObject;
guidata(hObject,handles);
But there is an error with this code. Is this coding are correct to answer my question?
Update:
I did this coding on Mytable1_CellEditCallback. It still have error. Is this code true??
Mytable1=get(hObject,'Data')
if isnan(Mytable1)
set(hObject, 'Data', 0);
h=errordlg('Oh noes!','Error');
set(h, 'WindowStyle', 'modal');
uiwait(h);
return
end
handles.Mytable2 = hObject;
guidata(hObject,handles);
This is the error:
Mytable1 =
[1] [] []
[] [] []
[] [] []
[] [] []
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
Error in ==> fyp_editor>Mytable1_CellEditCallback at 795 if ~isnan(Mytable1)
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)fyp_editor('Mytable1_CellEditCallback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uitable CellEditCallback
There are several errors in this code. Let me count the ways.
function Mytable1_CreateFcn(hObject, eventdata, handles)
The create function is executed at object creation, i.e. when the GUI is built. It is never executed otherwise, and thus it's a function you'll rarely ever want to modify. What you do want to modify is
Mytable1_Callback
if isnan(Mytable1)
Mytable1
has not been defined within the function. Thus, this line will give you an error. Maybe you meant to add a lineMytable1=get(hObject,'Data')
? Also, sinceMytable1
is a cell array, you have to check the elements for NaNs usingcellfun
, i.e. writeif any(cellfun(@isnan,Mytable1(:)))
.set(hObject, 'Data', 0);
This line is most likely fine.
errordlg('Input must be a number','Error');
While this line is not an error, it won't behave as intended - The message pops up, but the function continues executing. Either you should write
error('input must be a number)
, or writeh=errordlg('Oh noes!');uiwait(h);return
end
Look, another line that is fine!
handles.Mytable2 = hObject; guidata(hObject,handles);
With this, you overwrite the handle to
Mytable2
with the handle toMytable1
. Is that REALLY what you want?
if isnan(Mytable1)
Mytable is a cell array. Try this:
if any(isnan(cell2mat(Mytable1)))
However, there migth be an easier way, the Cell edit callback gets two inputs: the calling Object and the eventdata (and handles in guide). In the eventdata has following Fields:
Indices: 1-by-2 array containing the row and column indices of the cell the user edited.
PreviousData: Previous cell data. The default is an empty matrix, [].
EditData: User-entered string.
NewData: Value that MATLAB wrote to the Data property array. It is either the same as EditData or a converted value. The NewData property is empty if MATLAB detects an error in the user-entered data.
Error: Error message returned if MATLAB detects an error in the user-entered data. The Error property is empty when MATLAB successfully writes the value to the Data property. If the Error property is not empty, then the CellEditCallback can display the string, or it can attempt to fix the problem.
Source:http://ch.mathworks.com/help/matlab/ref/uitable-properties.html#zmw57dd0e748724
With NewData or Error this should be possible to handle error checking.
for example:
MyTable_CellEditCallback(hObj,event,handles)
if isempty(event.NewData)
h=errordlg('Oh noes!','Error');
set(h, 'WindowStyle', 'modal');
uiwait(h);
return
end
% The rest of the function
end
精彩评论