Error with matlab gui: Invalid axes handle when using to axes.

Hello everyone,
Im trying to build a gui for daq system. The idea is to make a gui that allows the user to generate signals. The output signal should be displayed on one axes (here axes1) and the acquired signal on the second axes (here axes2).
The problem occurs only when i try to send more signals after one another, and to avoid that error, i have to close the gui and run it again.
I can't write the full code, so will just write how i called the axes:-
...
...
...
axes(handles.axes1);
plot(....)
data=readwrite(dq.output);
.axes(handles.axes2);
plot(....).
------------------------------------------
The error says:
Error using axes
Invalid axes handle
Error in DAQ_GUI>....
axes(handles.axes2);
...
------------------------------------------
I have looked it up in google and some people were suggesting to do pause or hold on commands. I've tried using them but i didn't worked.
I would be very happy if some one can help me.
Regards
Ali

 Accepted Answer

It would appear handles.axes2 does not exist. If you have two axes in your gui, make sure you are using the correct name. If not, you must create handles.axes2 before you pass it into axes.

8 Comments

it does exist, because it displayes the acquired signal. the problem occures when i try to generate signals after one another.
Set an error breakpoint (or a regular breakpoint on the line of code where the error is thrown.) Run your code. When you reach the breakpoint, run this command:
handles.axes2
I'm guessing that either 1) handles.axes2 is not an axes handle or 2) it is a handle to a deleted axes.
ax1 = subplot(2, 2, 1);
ax2 = subplot(2, 2, [1 3]);
ax1 % Deleted because of the documented behavior of subplot
ax1 =
handle to deleted Axes
From the Tips section of the documentation page for subplot: "The subplot function deletes existing axes that overlap new axes."
It would also be helpful if you shared your actual code rather than the trimmed code above.
You are absloutly right.
Thank you very much.
it is true that it is saying handle to deleted axes. But this is still confusing me because sometimes it works fine and it only happens after i try to generate other signals.
I still don't know how to avoid the axes being deleted :(
So far we can only guess. Share your full code if you want an answer specific to you.
EDIT moving Ali's response here.
it is a code of more than 600 lines.
i will share a section:-
%----------------------------------------------
addinput(dq, "Dev2", "ai10", "Voltage");
addoutput(dq, "Dev2", "ao0", "Voltage");
TEST=A*sin(2*pi*f*t)';
%---------------------------------
axes(handles.axes1);
plot(t,TEST,'linewidth',2);
grid on
xlabel('t (s)')
ylabel('A (V)')
%----------
setappdata(0 ,'TEST',TEST)
%----------
data=readwrite(dq,TEST);
handles.axes2;
axes(handles.axes2);
plot(data.Time, data.Variables,'linewidth',2 )
ylim([-A A]);
title('Data')
grid on
%----------
setappdata(0 ,'Data',data)
%----------
Other section doesn't look much different.
This may be where the deletion is manifesting itself, but it is not where the axes is getting deleted. Use the paperclip to attach your files to your post.

Sign in to comment.

More Answers (2)

The issue arises when you use stackedplot. A stackedplot cannot be added to an existing axes. Instead it replaces the current axes with a new one created by stackedplot.
axes1 = axes;
axes(axes1);
plot(1:3)
axes(axes1)
stackedplot(rand(5,2))
% your error
axes(axes1)
Error using axes
Invalid axes handle
plot(2:4)
The workaround could be to reset the axes handle after you create a stacked plot, but you may also need to use clf to remove the axes labels added by stackedplot.
So the workaround code for the code above may be something like this.
axes1 = axes;
axes(axes1);
plot(1:3)
axes(axes1)
stackedplot(rand(5))
axes1 = axes; % assing new axes handle to axes1
clf(axes1) % clear axes formatting added by stackedplot
axes(axes1)
plot(2:4)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!