You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Invalid handle object, Please help!
6 views (last 30 days)
Show older comments
I am programming a function for a edit text box that allows the user to input a min and max for the axes in a graph:
function edit1_Callback(hObject, eventdata, handles)
%Input X-Min value for axes1
user_entry = str2double(get(hObject, 'String'));
if isnan(user_entry)
errordlg('You must enter a numeric value.')
uicontrol(hObject)
return
end
set(handles.axes1, 'XLim', [user_entry Inf]);
guidata(hObject, handles);
However, I get a error message saying "Invalid handle object." at the set line. I suspect it has to do with the 'handles.axes1', but I'm not sure how to fix it. Can anyone help? Thanks in advance
3 Comments
Walter Roberson
on 6 Jun 2013
You have not given us reason to expect that handles.axes1 was ever created, let alone that reason to think it should still be valid. We are going to need to see more of the code, including the code for every routine you invoke between start-up and the time you run into that problem in the callback.
Melvin
on 6 Jun 2013
When I perform 'which handles -all; disp(handles);' before the set line, I get the following:
handles is a variable.
figure1: 257.0094
uipanel1: 302.0093
uipanel2: 265.0094
uitoolbar1: 258.0094
edit12: 338.0093
edit11: 337.0093
edit10: 336.0093
edit9: 335.0093
edit8: 334.0093
edit7: 333.0093
edit6: 332.0093
edit5: 331.0093
edit4: 330.0093
edit3: 329.0093
edit2: 328.0093
edit1: 327.0093
text9: 326.0093
text8: 325.0093
text7: 324.0093
text3: 323.0093
text2: 322.0093
text1: 321.0093
popupmenu1: 320.0093
popupmenu3: 319.0093
popupmenu2: 318.0093
axes2: 313.0093
axes1: 308.0093
axes3: 303.0093
edit24: 301.0093
edit23: 300.0093
edit22: 299.0093
edit21: 298.0093
edit20: 297.0093
edit19: 296.0093
edit18: 295.0093
edit17: 294.0093
edit16: 293.0093
edit15: 292.0093
edit13: 291.0093
edit14: 290.0093
text10: 289.0093
text11: 288.0093
text12: 287.0093
text4: 286.0093
text5: 285.0093
text6: 284.0093
popupmenu5: 283.0093
popupmenu4: 282.0093
popupmenu6: 281.0093
axes5: 276.0093
axes4: 271.0093
axes6: 266.0094
uitoggletool5: 264.0094
uitoggletool3: 263.0094
uitoggletool4: 262.0094
uitoggletool2: 261.0094
uitoggletool1: 260.0094
uipushtool2: 259.0094
output: 257.0094
So does this mean that the axes1 is being deleted?
Answers (1)
Walter Roberson
on 6 Jun 2013
It appears then that handles.axes1 was initialized, but that the axes was destroyed before you use it.
The probable reason for that is as described at the beginning of http://www.mathworks.co.uk/matlabcentral/answers/78221-problem-with-axes-handle#answer_87936
7 Comments
Walter Roberson
on 6 Jun 2013
Which MATLAB version are you using?
At the MATLAB command line, give the command
help hold
Does the output include the line
hold(AX,...) applies the command to the Axes object AX.
??
If you give the command at the command line
dbstop if error
and then run, then when the program stops to report the error, at the K> prompt, give the command
dbup
until it shows that you are at your hold() call. At that point, please show the output of
ishandle(handles.axes1)
Walter Roberson
on 6 Jun 2013
I believe hold() accepted axes handles back then as well. But ishandle() showing 0 is a problem.
If you put a breakpoint in at the set(), run up to there and then test
ishandle(handles.axes1)
and also show the numeric value of handles.axes1
I suspect that we are going to need to see more code, of every routine involved in the set-up and every routine you invoke up until the edit1 callback is called.
Melvin
on 6 Jun 2013
Here is my full code up until the callback involved. The original value of handles.axes1 is 308.0093.
function varargout = opengui(varargin)
% OPENGUI MATLAB code for opengui.fig
% OPENGUI, by itself, creates a new OPENGUI or raises the existing
% singleton*.
%
% H = OPENGUI returns the handle to a new OPENGUI or the handle to
% the existing singleton*.
%
% OPENGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in OPENGUI.M with the given input arguments.
%
% OPENGUI('Property','Value',...) creates a new OPENGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before opengui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to opengui_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 opengui
% Last Modified by GUIDE v2.5 06-Jun-2013 06:31:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @opengui_OpeningFcn, ...
'gui_OutputFcn', @opengui_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 opengui is made visible.
function opengui_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 opengui (see VARARGIN)
% Choose default command line output for opengui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes opengui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = opengui_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 uipushtool2_ClickedCallback(hObject, eventdata, handles)
% hObject handle to uipushtool2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Opens file
[filename, pathname] = uigetfile('*.txt');
file = fullfile(pathname,filename);
S1 = importdata(file, ' ', 4);
S2 = importdata(file, ' ', length(S1.data)+length(S1.textdata)+1);
S3 = importdata(file, ' ', length(S2.data)+length(S2.textdata)+1);
x1 = S1.data(:,1);
y1 = S1.data(:,2);
x2 = S2.data(:,1);
y2 = S2.data(:,2);
x3 = S3.data(:,1);
y3 = S3.data(:,2);
titles = strmatch('Legend', S3.textdata);
a1 = subplot(2,2,1, 'Parent', handles.uipanel1);
handles.axes1 = plot(a1,x1,y1);
xlabel('Time (sec)'), ylabel('Amplitude'), title(S3.textdata(titles(1,1)));
a2 = subplot(2,2,2, 'Parent', handles.uipanel1);
handles.axes2 = plot(a2,x2,y2);
xlabel('Time (sec)'), ylabel('Amplitude'); title(S3.textdata(titles(2,1)));
a3 = subplot(2,2,[3 4], 'Parent', handles.uipanel1);
handles.axes3 = plot(a3,x3,y3);
xlabel('Time (sec)'), ylabel('Amplitude'); title(S3.textdata(titles(3,1)));
%Opens second file
[filename2, pathname2] = uigetfile('*.txt');
file2 = fullfile(pathname2,filename2);
B1 = importdata(file2, ' ', 4);
B2 = importdata(file2, ' ', length(B1.data)+length(B1.textdata)+1);
B3 = importdata(file2, ' ', length(B2.data)+length(B2.textdata)+1);
x4 = B1.data(:,1);
y4 = B1.data(:,2);
x5 = B2.data(:,1);
y5 = B2.data(:,2);
x6 = B3.data(:,1);
y6 = B3.data(:,2);
titles2 = strmatch('Legend', B3.textdata);
a4 = subplot(2,2,1, 'Parent', handles.uipanel2);
handles.axes4 = plot(a4,x4,y4);
xlabel('Time (sec)'), ylabel('Amplitude'), title(B3.textdata(titles2(1,1)));
a5 = subplot(2,2,2, 'Parent', handles.uipanel2);
handles.axes5 = plot(a5,x5,y5);
xlabel('Time (sec)'), ylabel('Amplitude'); title(B3.textdata(titles2(2,1)));
a6 = subplot(2,2,[3 4], 'Parent', handles.uipanel2);
handles.axes6 = plot(a6,x6,y6);
xlabel('Time (sec)'), ylabel('Amplitude'); title(B3.textdata(titles2(3,1)));
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
val = get(hObject, 'Value');
str = get(hObject, 'String');
switch str{val}
case 'Linear' %User chooses Linear
handles.axes1 = axes('XScale', 'linear');
case 'Logarithmic' %User chooses Log
handles.axes1 = axes('XScale', 'log');
end
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu6.
function popupmenu6_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu6 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu6
% --- Executes during object creation, after setting all properties.
function popupmenu6_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu4 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu4
% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu5.
function popupmenu5_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu5 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu5
% --- Executes during object creation, after setting all properties.
function popupmenu5_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in popupmenu9.
function popupmenu9_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu9 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu9
% --- Executes during object creation, after setting all properties.
function popupmenu9_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 edit1_Callback(hObject, eventdata, 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
%Input X-Min value for axes1
user_entry = str2double(get(hObject, 'String'));
if isnan(user_entry)
errordlg('You must enter a numeric value.')
uicontrol(hObject)
return
end
hold(handles.axes1, 'on');
ishandle(handles.axes1);
set(handles.axes1, 'XLim', [user_entry Inf]);
drawnow();
% xlim(handles.axes1,[user_entry Inf]); %second possible method
guidata(hObject, handles);
Walter Roberson
on 6 Jun 2013
Note that
handles.axes1 = axes('XScale', 'linear');
creates a new axis, rather than changing the current handles.axes1 to be linear.
Melvin
on 7 Jun 2013
Thanks for your help Walter.
I realized my problem this morning. The subplot function in the open file function was overwriting all of the axes handles I created in the GUI guide. So I removed them and instead placed my data in each by selecting each respective axes with the axes() function and then immediately plotting them.
See Also
Categories
Find more on Graphics Object Properties 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)