Clear axes in GUI using cla reset does not work for secondary axes?

Hi all,
I am having some trouble clearing an axes using the reset command below:
cla reset
I have created an axes with a secondary axis through the following command (the variables F, S, L and D are arrays):
set(get(handles.ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(handles.ax1); %make ax1 the current axis
x1 = F;
y1 = S;
y2 = L;
y3 = D;
hl1 = line(x1,y1,'Color','r'); %plot a line in the current axis
hl2 = line(x1,y2,'Color','g');
handles.ax2 = axes('Units', 'character'); %create a new axis and set units to be character
set(handles.ax2, 'Position',get(handles.ax1,'Position'),...
'YAxisLocation','right',...
'Color','none',...
'YColor','k'); %position the new axis on the earlier existing axis
hl3 = line(x1,y3,'Color','b','Parent',handles.ax2); %plot a line on the new axis
However, when I use the "cla reset" command, it does not reset the secondary axis I created, nor the data from the secondary axis.
How can I fix this?

 采纳的回答

Maarten - where are you calling the line of code to clear the axes? When you call this line, do the objects from the first axes get cleared, or does nothing happen?
If you want to explicitly indicate which axes to clear, then try the following
cla(handles.ax2,'reset');
With the above, you should be clearing all objects that have visible handles on the specified axes. If this doesn't work, then verify that you have drawn the graphics on the second axes.

8 个评论

I tried this command earlier, but it does not clear these axis. Like you say, it only clears the primary axis and objects.
I am calling this "cla reset" line before the axes updates. I press a push button, first "cla reset" so that the axes is clear. Then I load an Excel-file with parameters and these are set to the graph as shown in the code.
%Pushbutton callback ~
cla reset; % Do a complete and total reset of the axes.
G=xlsread('Test1.xlsx','G823','A3:D18');
F = G(:,1);
S = G(:,2);
L = G(:,3);
D = G(:,4);
set(get(handles.ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(handles.ax1); %make ax1 the current axis
x1 = F;
y1 = S;
y2 = L;
y3 = D;
hl1 = line(x1,y1,'Color','r'); %plot a line in the current axis
hl2 = line(x1,y2,'Color','g');
%set(ax1,'XColor','r','YColor','r'); %set the axis colors of ax1
handles.ax2 = axes('Units', 'character'); %create a new axis and set units to be character
set(handles.ax2, 'Position',get(handles.ax1,'Position'),...
'YAxisLocation','right',...
'Color','none',...
'YColor','k'); %position the new axis on the earlier existing axis
hl3 = line(x1,y3,'Color','b','Parent',handles.ax2); %plot a line on the new axis
I Think I have to re-arrange some lines, but I cannot find out how. Problem is:
I can reset axis 1 by the first line, but in this case axis 2 is not defined yet.
What if you add some code like the following?
% clear the first axes (if you really want to do this??)
cla(handles.ax1,'reset');
% clear or create the second axes
if ~isfield(handles,'ax2')
handles.ax2 = axes('Units', 'character');
set(handles.ax2, 'Position',get(handles.ax1,'Position'),...
'YAxisLocation','right',...
'Color','none',...
'YColor','k');
guidata(hObject,handles);
else
clear(handles.ax2,'reset');
end
The if statement either creates the second axes and saves it to the handles structure (using guidata) or just clears anything that has been "drawn" on it.
Hmm, that should work, however it can only clear handles.ax2 if these axis exist. So the first time when I push this button there is no ax2 yet. Therefor an error is generated, which says handles.ax2 does not exist and cannot be cleared.
I think it should be something like:
IF handles.ax2 exists it should be cleared and then again be created again. Elseif handes.ax2 does not exist yet, create handles.ax2.
Correct?
By the way: I wish everyone here a very happy new year!
Maarten - isn't that what the above code does (with the exception of the clear which should be a cla)? We are checking to see if the handles structure has the field ax2. If it doesn't, then the ax2 should be created. Note the tilde in front of the ~isfield indicating not. If it does exist, then it should be cleared. (I don't know why you would want to create it again.)
if ~isfield(handles,'ax2')
% second axes does not exist so create it!
else
% second axes does exist so clear it!
end
You are right, my bad. It almost works. I clear the axis and then create it again with new data by toggling the same push button.
The code is almost right. One last problem is: by toggling the push button again it clears the axis of ax2 and then sets new data to the same axis. However a new second axis is created over the ax1.
The rest of the code works perfect!
Maarten - I would have to see the code that you are using to try and figure out what is causing the problem. You aren't creating a new axes each time the push button is pressed, correct? The only time the second axes should be created is on the first button press.
Geoff, thanks for the help. I did not find the time to reply here, but in the end I used the following command:
cla(handles.ax1,'reset'); % Do a complete and total reset of the axes.
if isfield(handles,'ax2') %delete ax2 if it exists
delete(handles.ax2,'');
end

请先登录,再进行评论。

更多回答(3 个)

What if,
set(get(handles.ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(handles.ax1); %make ax1 the current axis
cla(handles.ax1,'reset');
set(get(handles.ax2, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(handles.ax2); %make ax1 the current axis
cla(handles.ax2,'reset');
delete(allchild(handles.axes1))

2 个评论

This code did not work for the ax2, but thanks for the input.
Almost the same code used, but I had to specify it to handles.ax2.
cla(handles.ax1,'reset'); % Do a complete and total reset of the axes.
if isfield(handles,'ax2')
delete(handles.ax2,'');
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by