Need help with Graphic interface

1 view (last 30 days)
Dionisio Mendoza
Dionisio Mendoza on 4 Apr 2019
Answered: Walter Roberson on 5 Apr 2019
On this graph iterface make make me this error:
>> ejerciciosistemasbiologicosinterfaz
Undefined operator '+' for input arguments of type 'matlab.ui.control.UIControl'.
Error in ejerciciosistemasbiologicosinterfaz>pushbutton1_Callback (line 138)
suma = handles.edit1 + handles.edit2;
Error in gui_mainfcn (line 95)
feval(varargin{:});
---------------------------------------------------------------------------------------------------------he program is this and error its on the las part:
function varargout = ejerciciosistemasbiologicosinterfaz(varargin)
% EJERCICIOSISTEMASBIOLOGICOSINTERFAZ MATLAB code for ejerciciosistemasbiologicosinterfaz.fig
% EJERCICIOSISTEMASBIOLOGICOSINTERFAZ, by itself, creates a new EJERCICIOSISTEMASBIOLOGICOSINTERFAZ or raises the existing
% singleton*.
%
% H = EJERCICIOSISTEMASBIOLOGICOSINTERFAZ returns the handle to a new EJERCICIOSISTEMASBIOLOGICOSINTERFAZ or the handle to
% the existing singleton*.
%
% EJERCICIOSISTEMASBIOLOGICOSINTERFAZ('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EJERCICIOSISTEMASBIOLOGICOSINTERFAZ.M with the given input arguments.
%
% EJERCICIOSISTEMASBIOLOGICOSINTERFAZ('Property','Value',...) creates a new EJERCICIOSISTEMASBIOLOGICOSINTERFAZ or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ejerciciosistemasbiologicosinterfaz_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ejerciciosistemasbiologicosinterfaz_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ejerciciosistemasbiologicosinterfaz
% Last Modified by GUIDE v2.5 04-Apr-2019 16:00:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ejerciciosistemasbiologicosinterfaz_OpeningFcn, ...
'gui_OutputFcn', @ejerciciosistemasbiologicosinterfaz_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ejerciciosistemasbiologicosinterfaz is made visible.
function ejerciciosistemasbiologicosinterfaz_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ejerciciosistemasbiologicosinterfaz (see VARARGIN)
% Choose default command line output for ejerciciosistemasbiologicosinterfaz
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ejerciciosistemasbiologicosinterfaz wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ejerciciosistemasbiologicosinterfaz_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
Val = str2double(get(hObject, 'String'));
if isnan (Val)
set (hObject, 'String', 0);
errordlg('Inggresa un numero por favor','Error');
end
handles.edit1 = Val;
guidata(hObject,handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
function edit2_Callback(hObject, eventdata, handles)
Val = str2double(get(hObject, 'String'));
if isnan (Val)
set (hObject, 'String', 0);
errordlg('Inggresa un numero por favor','Error');
end
handles.edit1 = Val;
guidata(hObject,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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
suma = handles.edit1 + handles.edit2;
set (handles.suma, 'String', suma);

Answers (1)

Walter Roberson
Walter Roberson on 5 Apr 2019
Your edit1_Callback sets handles.edit1 to a numeric value and then uses guidata() to update the master copy of the handles structure. After that point, handles.edit1 no longer refers to a uicontrol as it did before that point. This is a bad design: if you have happened to run edit1_Callback then handles.edit1 is numeric, but if you have not happened to run edit1_Callback then handles.edit1 is the handle of the uicontrol 'style', 'edit' that was put in place automatically when the GUI was loaded.
Likewise for edit1_Callback and handles.edit2 : if you did not happen to run edit2_Callback then handles.edit2 is still the handle of the uicontrol.
When you get to pusbutton1_Callback, your code assumes that both handles.edit1 and handles.edit2 have been overwritten with numeric values already. When that does not happen to be the case, you will see exactly the message you see now, about + not being a valid operation for uicontrol .
I recommend against overwriting handles.edit1 and handles.edit2 -- at the very least it is going to confuse the people reading the code.
What you can do is after validating the input is you can update the String property with the "clean" number, and then in the pushbutton you can str2double() the respective string properties and add those.
Or after validating the input you can update the UserData property of the uicontrol with the numeric value, and then in the pushbutton you can fetch the UserData property, substitute 0 if it is empty, and then add the values.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!