Using pop-up menu to graph different scenarios

2 次查看(过去 30 天)
Hello All!
I have a question regarding a pushbutton, pop-up menu, and axes.
I want three options in my pop up menu, and I want to be able to choose an option in the menu, then press the push button, then have it graph certain things.
Code below:
function varargout = example1(varargin)
% EXAMPLE1 MATLAB code for example1.fig
% EXAMPLE1, by itself, creates a new EXAMPLE1 or raises the existing
% singleton*.
%
% H = EXAMPLE1 returns the handle to a new EXAMPLE1 or the handle to
% the existing singleton*.
%
% EXAMPLE1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EXAMPLE1.M with the given input arguments.
%
% EXAMPLE1('Property','Value',...) creates a new EXAMPLE1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before example1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to example1_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 example1
% Last Modified by GUIDE v2.5 18-May-2019 10:11:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @example1_OpeningFcn, ...
'gui_OutputFcn', @example1_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 example1 is made visible.
function example1_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 example1 (see VARARGIN)
% Choose default command line output for example1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes example1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = example1_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)
axes(handles.axes2)
if popVal==1
%axes(handles.axes2)
clear, close all
syms x(t)
Dx=diff(x)
D2x=diff(x,2)
ode=1*D2x+1*Dx+3*x==0
xSol1 = dsolve(ode,x(0)==1, Dx(0)==0)
hold on
axis([-5 10 -8 6.8])
fplot(xSol1,'k','LineWidth',2)
title('x''''+x''+3*x=0')
%Obviously I want to to do more operations, but I cannot get this to work.
%elseif handles.op==3
% handles.ab=handles.a*handles.b
% set(handles.c,'String',num2str(handles.ab))
%%elseif handles.op==4
% handles.ab=handles.a/handles.b
% set(handles.c,'String',num2str(handles.ab))
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popChoice=contents{get(hObject,'Value')};
under over critical
if(strcmp(popChoice,'Underdamped'))
popVal=1
elseif (strcmp(popChoice,'Overdamped'))
popVal=2
elseif (strcmp(popChoice,'Critical'))
popVal=3
end
assign('base','popVal',popVal);
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

回答(1 个)

Geoff Hayes
Geoff Hayes 2019-5-19
William - there is no need to dynamically create a variable in the base work space to handle the selection in the pop-up menu. In fact, you don't even need a callback for this menu unless you need something to occur immediately upon user selection of the menu. Instead, add code to your pushbutton callback to get the selection from the pop-up menu. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
contents=cellstr(get(handles.popupmenu1,'String'));
popChoice=contents{get(handles.popupmenu1,'Value')};
popVal = 0;
if(strcmp(popChoice,'Underdamped'))
popVal=1
elseif (strcmp(popChoice,'Overdamped'))
popVal=2
elseif (strcmp(popChoice,'Critical'))
popVal=3
end
% and the rest of the code is that from your pushbutton callback
Note how we just moved the body of the pop-up menu callback to the pushbutton callback and are using the handles.popupmenu1 (which is the handle to the menu) to access the selected option.

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by