-3

suppose that I have gui code which generates a plot by ticking a checkbox. how can I delete the drawn graph by removing the tick. thanks in advance.

So this is my code, step1 and sound are two checkboxes that generate different figures according to the radio buttons. can you please help me to do the same that you did with my m.file? I really appreciate

function varargout = RotPlot(varargin)
%ROTPLOT M-file for RotPlot.fig
%      ROTPLOT, by itself, creates a new ROTPLOT or raises the existing
%      singleton*.
%
%      H = ROTPLOT returns the handle to a new ROTPLOT or the handle to
%      the existing singleton*.
%
%      ROTPLOT('Property','Value',...) creates a new ROTPLOT using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to RotPlot_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      ROTPLOT('CALLBACK') and ROTPLOT('CALLBACK',hObject,...) call the
%      local function named CALLBACK in ROTPLOT.M with the given input
%      arguments.
%
%      *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 RotPlot

% Last Modified by GUIDE v2.5 07-Mar-2016 13:59:41

% Begin initialization code - DO NOT EDIT

    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @RotPlot_OpeningFcn, ...
                       'gui_OutputFcn',  @RotPlot_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 RotPlot is made visible.
function RotPlot_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

movegui('center')
set(hObject , 'Name' , varargin{2})
setappdata(0 , 'Axial1' , varargin{3})
setappdata(0 , 'Axial2' , varargin{4})
setappdata(0 , 'Tor1' , varargin{5})
setappdata(0 , 'Tor2' , varargin{6})
setappdata(0 , 'sheary1' , varargin{7})
setappdata(0 , 'sheary2' , varargin{8})
setappdata(0 , 'shearz1' , varargin{9})
setappdata(0 , 'shearz2' , varargin{10})
setappdata(0 , 'momenty1' , varargin{11})
setappdata(0 , 'momenty2' , varargin{12})
setappdata(0 , 'momentz1' , varargin{13})
setappdata(0 , 'momentz2' , varargin{14})


% Choose default command line output for RotPlot
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes RotPlot wait for user response (see UIRESUME)
uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = RotPlot_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


% --- Executes during object creation, after setting all properties.
function SecForce_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SecForce (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --------------------------------------------------------------------
function SecForce_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to SecForce (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when selected object is changed in SecForce.
function SecForce_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in SecForce 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in closebutton.
function closebutton_Callback(hObject, eventdata, handles)
% hObject    handle to closebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
delete(handles.figure1)


% --- Executes on button press in sound.
function sound_Callback(hObject, eventdata, handles)
% hObject    handle to sound (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of sound
Axial1 = getappdata(0 , 'Axial1' );
Tor1 = getappdata(0 , 'Tor1');
sheary1 = getappdata(0 , 'sheary1');
shearz1 = getappdata(0 , 'shearz1');
momenty1 = getappdata(0 , 'momenty1');
momentz1 = getappdata(0 , 'momentz1');
if (get(hObject , 'Value') == get(hObject , 'Max'))
    switch get(get(handles.SecForce , 'selectedObject') ,'Tag')
        case 'axial'
           p1 =  plot(Axial1 , 'red');
           xlabel('Sections')
           ylabel('Axial Force (Kg)')
           title('Axial Force')
            hold on
        case 'tor'
            p2 = plot(Tor1 , 'red');
              xlabel('Sections')
           ylabel('Torsion (Kg.cm)')
           title('Torsion')
            hold on
        case 'sheary'
            p3 = plot(sheary1 , 'red');
              xlabel('Sections')
           ylabel('Shear (Kg)')
           title('Shear Force in xy plane')
            hold on
        case 'shearz'
            p4 = plot(shearz1 , 'red');
                  xlabel('Sections')
           ylabel('Shear (Kg)')
           title('Shear Force in xz plane')
            hold on
        case 'momenty'
            p5 = plot(momenty1 , 'red');
                  xlabel('Sections')
           ylabel('Moment (Kg.cm)')
           title('Moment about y axis')
            hold on
        case 'momentz'
            p6 = plot(momentz1 , 'red');
                 xlabel('Sections')
           ylabel('Moment (Kg.cm)')
           title('Moment about z axis')
            hold on
    end  
end
visibility = {'off', 'on'};
set(p1, 'Visible', visibility{get(hObject, 'value') + 1})

% --- Executes on button press in step1.
function step1_Callback(hObject, eventdata, handles)
% hObject    handle to step1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of step1
Axial2 = getappdata(0 , 'Axial2');
Tor2 = getappdata(0 , 'Tor2');
sheary2 = getappdata(0 , 'sheary2');
shearz2 = getappdata(0 , 'shearz2');
momenty2 = getappdata(0 , 'momenty2');
momentz2 = getappdata(0 , 'momentz2');

if (get(hObject , 'Value') == get(hObject , 'Max'))
    switch get(get(handles.SecForce , 'selectedObject') ,'Tag')
        case 'axial'
           p7 =  plot(Axial2 , 'blue');
               xlabel('Sections')
           ylabel('Axial Force (Kg)')
           title('Axial Force')
            hold on
        case 'tor'
         p8 = plot(Tor2 , 'blue');
            xlabel('Sections')
           ylabel('Torsion (Kg.cm)')
           title('Torsion')
            hold on
        case 'sheary'
           p9 =  plot(sheary2 , 'blue');
               xlabel('Sections')
           ylabel('Shear (Kg)')
           title('Shear Force in xy plane')
            hold on
        case 'shearz'
          p10 =  plot(shearz2 , 'blue');
             xlabel('Sections')
           ylabel('Shear (Kg)')
           title('Shear Force in xz plane')
            hold on
        case 'momenty'
           p11 =  plot(momenty2 , 'blue');
               xlabel('Sections')
           ylabel('Moment (Kg.cm)')
           title('Moment about y axis')
            hold on
        case 'momentz'
            p12 = plot(momentz2 , 'blue');
             xlabel('Sections')
           ylabel('Moment (Kg.cm)')
           title('Moment about z axis')
            hold on
    end
end


% --- Executes on button press in axial.
function axial_Callback(hObject, eventdata, handles)
% hObject    handle to axial (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of axial
cla


% --- Executes on button press in tor.
function tor_Callback(hObject, eventdata, handles)
% hObject    handle to tor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of tor
cla


% --- Executes on button press in sheary.
function sheary_Callback(hObject, eventdata, handles)
% hObject    handle to sheary (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of sheary
cla


% --- Executes on button press in shearz.
function shearz_Callback(hObject, eventdata, handles)
% hObject    handle to shearz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of shearz
cla


% --- Executes on button press in momenty.
function momenty_Callback(hObject, eventdata, handles)
% hObject    handle to momenty (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of momenty
cla


% --- Executes on button press in momentz.
function momentz_Callback(hObject, eventdata, handles)
% hObject    handle to momentz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of momentz
cla


% --- Executes on key press with focus on sound and none of its controls.
function sound_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to sound (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL)
%   Key: name of the key that was pressed, in lower case
%   Character: character interpretation of the key(s) that was pressed
%   Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
16
  • Detect when the tick is clicked, and clf, I guess Mar 7, 2016 at 12:56
  • I think no, I have tried it before
    – KratosMath
    Mar 7, 2016 at 12:57
  • and what about clf(figurehandle)? Mar 7, 2016 at 12:58
  • I don't know. I need something practical with codes.
    – KratosMath
    Mar 7, 2016 at 13:01
  • 3
    There is a problem if you dont know: you havent tried. We are not going to write the code for you! Best case scenario we will try to improve code that you posted, but you havent. Mar 7, 2016 at 13:03

1 Answer 1

4

If you want to toggle the visibility of something (in your case plot objects) when the box is checked/unchecked, you can do something like the following. I set the Callback for each checkbox to toggle the Visible property of plot to which it is linked. This can really be adapted to toggle the visibility of any MATLAB graphics objects. You just have to change what handle is used in the callbacks.

fig = figure();
hax = axes('Parent', fig, 'Position', [0.05 0.25 0.9 0.65]);
hplot1 = plot(rand(10, 1));
hold(hax, 'on')
hplot2 = plot(rand(10, 1));

hcheck1 = uicontrol('Style', 'check', ...
                   'parent', fig, ...
                   'value',1, ...
                   'Position', [10 10 200 20], ...
                   'String', 'Show Plot 1');

hcheck2 = uicontrol('Style', 'check', ...
                   'parent', fig, ...
                   'value', 1, ...
                   'Position', [220 10 200 20], ...
                   'String', 'Show Plot 2');

visibility = {'off', 'on'};

set(hcheck1, 'Callback', @(s,e)set(hplot1, 'Visible', visibility{get(s, 'value') + 1}))
set(hcheck2, 'Callback', @(s,e)set(hplot2, 'Visible', visibility{get(s, 'value') + 1}))

enter image description here

4
  • your code is correct, but my condition is different. in my case I have multiple lines in one figure and I would like to be able to make each one of them appear and disappear using related checkboxes. In that case, that do you suggest.
    – KratosMath
    Mar 7, 2016 at 13:57
  • @MsenRezaee That's precisely what my answer does...
    – Suever
    Mar 7, 2016 at 13:59
  • I have written my code on the question. can you please help me do the same thing on my code? I appreciate
    – KratosMath
    Mar 7, 2016 at 14:31
  • @MsenRezaee I'll leave that as an exercise for you. But looking at your code look carefully at what the handles in my answer are. You want to set the callback on the checkbox (currently you're doing it on the plot) and then the handle that your'e setting the visibility on is the plot (currently you're using hObject). Tweak those and try to get that to work.
    – Suever
    Mar 7, 2016 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.