Clearing plot in GUI axes

5 vues (au cours des 30 derniers jours)
Murali Krishna
Murali Krishna le 27 Août 2015
Commenté : Reza Lashani le 30 Mai 2022
I have 10 variables with data which I want to plot against time. I generated checkboxes with the corresponding name of the variable and axis in a figure. when the user checks a checkbox, the data corresponding to the variable it to be plotted with time series. I am using plot command in the checkbox callback function and was able to plot when it was checked. But when the checkbox is unchecked the plot still remains in the figure. I used cla command to clear the plot. The problem is when multiple checkboxes are selected and if one checkbox is unchecked all the plots are being cleared. I need to clear the plot only for which the checkbox is unchecked. kindly help... Thanks in advance
  3 commentaires
Image Analyst
Image Analyst le 30 Mai 2022
Modifié(e) : Image Analyst le 30 Mai 2022
@Reza Lashani in the callback for each individual checkbox, clear the one axes that corresponds to that checkbox. For example in the callback for chkClearAxes1, have
if handles.chkClearAxes1.Value
cla(handles.axes1, 'reset');
end
Do it similarly for all the other checkboxes.
Reza Lashani
Reza Lashani le 30 Mai 2022
yes but this command clears all plotted diagrams in axes. there is already other diagrams plotted in the same axes, I just want to delete one of them after the checkbox is unchecked and plot it when it is checked.
I have tried such code: (The checkbox is unchecked at the begining)
function CheckBoxDiagram_Callback(hObject, eventdata, handles)
x = 0: 0.02: 1;
y = sin(x);
if get(handles.CheckBoxDiagram, 'value')
Diagram = plot(handles.axes1, x, y, 'r--');
else
delete(Diagram)
end
But I get this error:
Undefined function or variable "Diagram".

Connectez-vous pour commenter.

Réponses (1)

Brendan Hamm
Brendan Hamm le 27 Août 2015
You need only get the plot objects (or handles prior to 2014b) to delete that individual plot.
f = figure;
a = axes('Parent',f);
x = linspace(0,2*pi);
hold on
for k = 1:3
p(k) = plot(a,x,sin(x/k));
end
We can now just delete the individual plot object:
delete(p(1))
It seems you may have a check box for each possible plot, so you may want to just initialize a vector of plot objects:
p = gobjects(numOfPlots,1);
so that you can always assign the plot from the first check box to p(1) and from the nth checkbox to p(n).
  4 commentaires
Murali Krishna
Murali Krishna le 21 Sep 2015
Dear Sir please find the code I am using. I am not able to delete individual plot.
function varargout = test1(varargin)
% TEST1 M-file for test1.fig
% TEST1, by itself, creates a new TEST1 or raises the existing
% singleton*.
% H = TEST1 returns the handle to a new TEST1 or the handle to
% the existing singleton*.
% TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST1.M with the given input arguments.
% TEST1('Property','Value',...) creates a new TEST1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before test1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to test1_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 test1
% Last Modified by GUIDE v2.5 17-Aug-2015 13:49:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test1_OpeningFcn, ...
'gui_OutputFcn', @test1_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
clc;
% --- Executes just before test1 is made visible.
function test1_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 test1 (see VARARGIN)
% Choose default command line output for test1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test1_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;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
%close all; run test1;
filespec = {'*.xlsx', 'Excelfiles (*.xlsx)';...
'*.xml', 'XML files (*.xml)';...
'*.xls', 'Excelfiles (*.xls)';};
[filename, pathname] = uigetfile(filespec, 'Open');
if isequal(filename,0)
return;
else
uicontrol('Style','text',...
'fontweight','bold',...
'fontsize',11,...
'String', 'Filename',...
'fontangle','normal',...
'horizontalalignment','left',...
'Position',[600,850,70,15]);
uicontrol('Style','text',...
'fontweight','normal',...
'fontsize',10,...
'fontangle','italic',...
'horizontalalignment','left',...
'String', filename,...
'Position',[680,850,600,15]);
[num, txt]= xlsread(filename);
analog=zeros(1,length(txt));
digital=zeros(1,length(txt));
for t=2:length(txt)
if max(num(:,t))>1
analog(t)=1;
else
digital(t)=1;
end
chckbox(t)=uicontrol('Style','checkbox',...
'String',txt(t),...
'Position',[300 800-20*t 150 20],...
'Callback',{@checkBox_Callback,txt,t,num,handles,analog,digital});
end
end
% --------------------------------------------------------------------
function text_Callback(hObject, eventdata, handles)
% hObject handle to text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function graphic_Callback(hObject, eventdata, handles)
% hObject handle to graphic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function checkBox_Callback(hObject,eventdata,txt,index, values,handles,analog,digital)
colour=['r' 'b' 'm' 'k' 'g' 'r' 'b' 'm' 'k' 'g' 'r' 'b' 'm' 'k' 'g' 'r' 'b' 'm' 'k' 'g' 'r' 'b' 'm' 'k' 'g'];
p=gobjects(length(txt),1);
if ((get(hObject,'value')==1)&&(analog(index)==1))
p(index)=plot(handles.axes2,values(:,1),values(:,index),colour(index));hold on;zoom on;
l=legend(handles.axes2,txt(index),'Location','NorthOutside');set(l,'interpreter','none');
elseif ((get(hObject,'value')==0&&(analog(index)==1)))
delete(handles.axes2,p(index));legend(handles.axes2,'off');
end
if ((get(hObject,'value')==1)&&(digital(index)==1))
plot(handles.axes3,values(:,1),values(:,index),colour(index));hold on;zoom on;
ll=legend(handles.axes3,txt(index),'Location','NorthOutside');set(ll,'interpreter','none');
elseif((get(hObject,'value')==0)&&(digital(index)==1))
cla(handles.axes3);legend(handles.axes3,'off');
end
linkaxes([handles.axes2 handles.axes3],'x');
I am getting the following error when unchecking the checkbox
Error using delete
Root object may not be deleted
Error in test1>checkBox_Callback (line 119)
delete(p(index));legend(handles.axes2,'off');
Error while evaluating uicontrol Callback
Thanks in advance
Brendan Hamm
Brendan Hamm le 21 Sep 2015
Presumably you have replaced the line:
p=gobjects(length(txt),1);
with
p = zeros(length(txt),1);
as you mention you are using 2007b.
In this case if you do not assign to one of the elements of p, the value of this element remains 0. When you call:
delete(0)
you will get an error as 0 is used for the root (that is information from your graphics card pertaining to your display). You could place another conditional to ensure that p(index) ~= 0 before deleting in this case.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by