假设我有gui代码,通过勾选复选框生成一个图。如何通过删除勾选来删除绘制的图形。 提前谢谢。
所以这是我的代码,step1和sound是两个复选框,根据单选按钮生成不同的数字。 你可以帮我做一下你用m.file做的事情吗? 我真的很感激
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)
答案 0 :(得分:4)
如果要在选中/取消选中此框时切换某些内容的可见性(在您的情况下为绘图对象),则可以执行以下操作。我为每个复选框设置Callback
以切换与其关联的绘图的Visible
属性。这可以真正适用于切换任何 MATLAB图形对象的可见性。您只需要更改回调中使用的句柄。
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}))