Clear Filters
Clear Filters

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

23 views (last 30 days)
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

Cris LaPierre
Cris LaPierre on 9 Feb 2022
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
Cris LaPierre
Cris LaPierre on 12 Feb 2022
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.
Cris LaPierre
Cris LaPierre on 12 Feb 2022
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)

Ali Albaidhani
Ali Albaidhani on 12 Feb 2022
Here are the script and fig files.

Cris LaPierre
Cris LaPierre on 12 Feb 2022
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)

Community Treasure Hunt

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

Start Hunting!