Error using handle/handle set
5 views (last 30 days)
Show older comments
Good afternoon I would like to know what the error is since it does not let me use the handles
this is my code
function varargout = calc3(varargin)
% CALC3 MATLAB code for calc3.fig
% CALC3, by itself, creates a new CALC3 or raises the existing
% singleton*.
%
% H = CALC3 returns the handle to a new CALC3 or the handle to
% the existing singleton*.
%
% CALC3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CALC3.M with the given input arguments.
%
% CALC3('Property','Value',...) creates a new CALC3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before calc3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to calc3_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 calc3
% Last Modified by GUIDE v2.5 26-Dec-2021 11:09:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @calc3_OpeningFcn, ...
'gui_OutputFcn', @calc3_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 calc3 is made visible.
function calc3_OpeningFcn(hObject, eventdata, handles, varargin)
axes('Units','normalized','Position',[0 0 1 1]);
x=imread('cilindro.jpg');
image(x),axis off
% 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 calc3 (see VARARGIN)
% Choose default command line output for calc3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes calc3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = calc3_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=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit1=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% 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=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit2=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% 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
function edit3_Callback(hObject, eventdata, handles)
Val=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit3=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
Val=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit4=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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)
calc1
clear,clc,close(calc3)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
ini=char(' ');
set(handles.edit2,'String',ini);
set(handles.edit2,'String',ini);
set(handles.edit3,'String',ini);
set(handles.edit4,'String',ini);
set(handles.edit5,'String',ini);
set(handles.edit6,'String',ini);
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
A=handles.edit1;
B=handles.edit2;
C=handles.edit3;
D=handles.edit5;
E=handles.edit4;
F=handles.edit6;
gapmax=A-D-((E+B)/2)+F;
set(handles.text8,'String',gapmax);
gapmin=D-((C-B)/2);
set(handles.text10,'String',gapmin);
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit5_Callback(hObject, eventdata, handles)
Val=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit5=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% hObject handle to edit5 (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 edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
Val=get(hObject,'String'); % Almacenar valor ingresado
NewVal=str2double(Val); % Transformar a formato double
handles.edit6=NewVal; % Almacenar enidentificador
guidata(hObject,handles); % Salvar datos de la aplicación
% hObject handle to edit6 (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 edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (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 checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
and this comes out as an error
Error using handle.handle/set
Invalid or deleted object.
Error in calc3>pushbutton3_Callback (line 203)
set(A,'String','');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in calc3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)calc3('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
0 Comments
Answers (1)
Voss
on 27 Dec 2021
The error happens because you are overwriting the edit uicontrols' handles in their callbacks. For instance:
function edit5_Callback(hObject, eventdata, handles)
% get handles.edit5 'String' property and store it in Val:
Val=get(hObject,'String'); % Almacenar valor ingresado
% convert to numeric and store in NewVal:
NewVal=str2double(Val); % Transformar a formato double
% replace handles.edit5 with NewVal:
handles.edit5=NewVal; % Almacenar enidentificador
% update the handles structure in the GUI:
guidata(hObject,handles); % Salvar datos de la aplicación
Now the next time you refer to "handles.edit5" it refers to the numeric scalar created here, rather than the handle to the uicontrol that used to be handles.edit5.
The solution is to avoid overwriting the handles to your uicontrol objects. If you want to store information about them in the handles structure, use different field names. For instance:
function edit5_Callback(hObject, eventdata, handles)
% get handles.edit5 'String' property and store it in Val:
Val=get(hObject,'String'); % Almacenar valor ingresado
% convert to numeric and store in NewVal:
NewVal=str2double(Val); % Transformar a formato double
% store NewVal in the handles structure as edit5_value:
handles.edit5_value=NewVal; % Almacenar enidentificador
% update the handles structure in the GUI:
guidata(hObject,handles); % Salvar datos de la aplicación
then use "handles.edit5" when you want to refer to the uicontrol, and use "handles.edit5_value" when you want to refer to the numeric value of the String of handles.edit5.
0 Comments
See Also
Categories
Find more on Function Creation 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!