Question: MATLAB Quesition - How do I you write callback functions in GUI for: 1) bisect method, false position method, and secant method 2) update all
MATLAB Quesition - How do I you write callback functions in GUI for:
1) bisect method, false position method, and secant method
2) update all / reset all
3) xl/xu/es changes
4) xmin/xmax menu changes
5) iterations and popup menu/function changes
6) User function selection from file system
Below is the matlab code for the GUI
function varargout = fb3779_Lab4(varargin) % FB3779_LAB4 MATLAB code for fb3779_Lab4.fig % FB3779_LAB4, by itself, creates a new FB3779_LAB4 or raises the existing % singleton*. % % H = FB3779_LAB4 returns the handle to a new FB3779_LAB4 or the handle to % the existing singleton*. % % FB3779_LAB4('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FB3779_LAB4.M with the given input arguments. % % FB3779_LAB4('Property','Value',...) creates a new FB3779_LAB4 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before fb3779_Lab4_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to fb3779_Lab4_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 fb3779_Lab4
% Last Modified by GUIDE v2.5 05-Mar-2015 18:37:25
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @fb3779_Lab4_OpeningFcn, ... 'gui_OutputFcn', @fb3779_Lab4_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 fb3779_Lab4 is made visible. function fb3779_Lab4_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 fb3779_Lab4 (see VARARGIN)
% Choose default command line output for fb3779_Lab4 handles.output = hObject;
%* example code by rj genik ii 26 Feb 2014 %* modified by rj genik ii 21 Oct 2014 %* xmin = 0; %default range for plot functions xmax = 3*pi; xsteps = 50; xPlot = linspace(xmin,xmax,xsteps); handles.xsteps = xsteps; handles.xdata = xPlot; handles.cosinePlotData = cos(xPlot); handles.sinePlotData = sin(xPlot); handles.userFunctionPlot Data = xPlot-(xmax-xmin)/pi; % initialize to x = y shifted to intercept within range handles.hcos = @cos; handles.hsin = @sin; handles.huser = @userFunction; % this file should exist handles.currentPlot_ydata = handles.cosinePlotData; handles.currentPlot_xdata = handles.xdata; handles.current_hf = handles.hcos; % set xmin and xmax in GUI set(handles.xminEdit,'Value',xmin); set(handles.xminEdit,'String',num2str(xmin,'%5.1f')); % converts number to string for display set(handles.xmaxEdit,'Value',xmax); set(handles.xmaxEdit,'String',num2str(xmax,'%5.1f')); % Plot function for inspection axes(handles.userFunctionPlot); plot(handles.currentPlot_xdata,handles.currentPlot_ydata,'b-'); hold on; %grid plot(handles.currentPlot_xdata,zeros(length(handles.currentPlot_xdata)),'k-'); hold off; %set some initial values for xl and xu, just using same set(handles.xlEdit,'Value',xmin); set(handles.xlEdit,'String',num2str(xmin,'%d')); set(handles.xuEdit,'Value',xmax); set(handles.xuEdit,'String',num2str(xmax,'%d')); % set initial values for es, maxiter, niterEdit and niterText set(handles.epssEdit,'Value',0.001); set(handles.imaxEdit,'Value',1000); set(handles.niterEdit,'Value',0); set(handles.niterText,'Value',0); set(handles.epssEdit,'String','0.001'); set(handles.imaxEdit,'String','1000'); set(handles.niterEdit,'String','0'); set(handles.niterText,'String','0'); % plot function for root finding xl = get(handles.xlEdit,'Value'); xu = get(handles.xuEdit,'Value'); xrdata = linspace(xl,xu,xsteps); yrdata =handles.current_hf(xrdata); axes(handles.rootAxes); hold on; plot(xrdata,yrdata,'b-'); plot(xrdata,zeros(length(xrdata)),'k-'); hold off; %display initial guesses for xrObject and errors handles.xr = (xu-xl)/2; handles.Ea = abs(xu-handles.xr); handles.ea = handles.Ea/handles.xr; xrstring = sprintf('%4.1f +/-%4.1f (%4.1f%%)', handles.xr, handles.Ea,handles.ea*100); set(handles.xrText,'Value',handles.xr); set(handles.xrText,'String',xrstring); % Update handles structure guidata(hObject, handles);
% UIWAIT makes fb3779_Lab4 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = fb3779_Lab4_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 bisect. function bisect_Callback(hObject, eventdata, handles) % hObject handle to bisect (see GCBO) % 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 fposition. function fposition_Callback(hObject, eventdata, handles) % hObject handle to fposition (see GCBO) % 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 secant. function secant_Callback(hObject, eventdata, handles) % hObject handle to secant (see GCBO) % 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 pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in listbox1. function listbox1_Callback(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array % contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties. function listbox1_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
function xminEdit_Callback(hObject, eventdata, handles) % hObject handle to text6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of text6 as text % str2double(get(hObject,'String')) returns contents of text6 as a double
% --- Executes during object creation, after setting all properties. function xuEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to text6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function xmaxEdit_Callback(hObject, eventdata, handles) % hObject handle to xmaxEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of xmaxEdit as text % str2double(get(hObject,'String')) returns contents of xmaxEdit as a double
% --- Executes during object creation, after setting all properties. function xmaxEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to xmaxEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function xlEdit_Callback(hObject, eventdata, handles) % hObject handle to xlEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of xlEdit as text % str2double(get(hObject,'String')) returns contents of xlEdit as a double
% --- Executes during object creation, after setting all properties. function xlEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to xlEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function xuEdit_Callback(hObject, eventdata, handles) % hObject handle to text6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of text6 as text % str2double(get(hObject,'String')) returns contents of text6 as a double
% --- Executes during object creation, after setting all properties. function text6_CreateFcn(hObject, eventdata, handles) % hObject handle to text6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
% --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties. 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
% --- Executes on button press in updateAll. function updateAll_Callback(hObject, eventdata, handles) % hObject handle to updateAll (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
function imaxEdit_Callback(hObject, eventdata, handles) % hObject handle to imaxEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of imaxEdit as text % str2double(get(hObject,'String')) returns contents of imaxEdit as a double
% --- Executes during object creation, after setting all properties. function imaxEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to imaxEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function niterEdit_Callback(hObject, eventdata, handles) % hObject handle to niterEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of niterEdit as text % str2double(get(hObject,'String')) returns contents of niterEdit as a double
% --- Executes during object creation, after setting all properties. function niterEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to niterEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function epssEdit_Callback(hObject, eventdata, handles) % hObject handle to epssEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of epssEdit as text % str2double(get(hObject,'String')) returns contents of epssEdit as a double
% --- Executes during object creation, after setting all properties. function epssEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to epssEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
% --- Executes during object creation, after setting all properties. function xminEdit_CreateFcn(hObject, eventdata, handles) % hObject handle to xminEdit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
