The Matlab GUI is event driven, which means that doing stuff implies setting the callback of a graphic object like an uicontrol
with the style pushbutton
(for example) to implement or call your custom code. This means: every time you'll click on the button, your custom code will be run.
MATLAB GUI Programming with ODE
-
04-04-2022 - |
Question
I am new to programming and I have to create a matlab gui code with ordinary differential equations (ODE) then graph said equation. The problem I am having is the format where a function for the ode is defined in one .m file and called in another .m file, like so:
% First part
% This is the ode .m file
function dydt=project(t,y)
dydt=zeros(3,1);
y(1);
y(2);
y(3);
dydt(1) = (some math);
dydt(2) = (some math);
dydt(3) = (some math);
end
% Second part
% This is a call for the ode function defined in the above .m file
% This can be in another .m file or called from command window
[t,y]=ode45('project',[0 10],[9.8 6591 61.3]);
My code requires that I create a gui which will initialize the ode function as soon as the gui program starts (first part) and will then take in the initial conditions and calculate the call to the function as soon as a button is pressed (second part).
My questions are: Do I have to call a .m file or is there a way to code the above ode function into the gui code? How do I go about entering the second part into the gui code?
Thanks for all those who contribute.
Edit: The code below is the GUIDE code for the gui used feel free to test it out and confirm the errors.
function varargout = reaction_kinetics(varargin)
% REACTION_KINETICS MATLAB code for reaction_kinetics.fig
%REACTION_KINETICS, by itself, creates a new REACTION_KINETICS or raises the existing
% singleton*.
%
%H = REACTION_KINETICS returns the handle to a new REACTION_KINETICS or the handle to
% the existing singleton*.
%
%REACTION_KINETICS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in REACTION_KINETICS.M with the given input arguments.
%
%REACTION_KINETICS('Property','Value',...) creates a new REACTION_KINETICS or raises
%the existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before reaction_kinetics_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to reaction_kinetics_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 reaction_kinetics
% Last Modified by GUIDE v2.5 16-Apr-2013 18:44:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @reaction_kinetics_OpeningFcn, ...
'gui_OutputFcn', @reaction_kinetics_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 reaction_kinetics is made visible.
function reaction_kinetics_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 reaction_kinetics (see VARARGIN)
% Perform first set of ODE's
function dydt=project(t,y)
dydt=zeros(3,1);
glc=y(1);
gsh=y(2);
gsg=y(3);
dydt(1)= (-5400*((924*glc)-(gsh/5600)))/((22*300)+(glc*300)+924*22*(1+(glc/22)+(gsh/30)));
dydt(2)=(5400*((924*glc)-(gsh/5600)))/((22*300)+(glc*300)+924*22*(1+(glc/22)+(gsh/30)))-(150*gsh/(150+gsh))-(1100*(gsh^3)/((3000^3)+(gsh^3)))-((2*4500*.01*gsh)/((1330+gsh)*(0.09+0.01)))+((2*8925*gsg*50)/((107+gsg)*(10.4+50)))-0.002*gsh;
dydt(3)=(4500*.01*gsh)/((1330+gsh)*(0.09+0.01))- ((8925*gsg*50)/((107+gsg)*(10.4+50)))-((40*gsg)/(1250+gsg))-((4025*gsg)/(7100+gsg))-0.1*gsg;
end
handles.project=project;
handles.current_data=handles.project;
% Choose default command line output for reaction_kinetics
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes reaction_kinetics wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = reaction_kinetics_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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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 popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
val=get(hObject,'Value');
str=get(hObject,'String');
switch str{val}
case 'GSH Half Life' % User selects GSH Half Life
handles.current_data=handles.plot(t,y);
case 'GSH v Glc' % User selects GSH v Glc
handles.current_data=handles.plot(y(2),y(1));
case 'GSH v GSSG' % User selects GSH v GSSG
handles.current_data=handles.plot(y(2),y(3));
end
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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 plot_button.
function plot_button_Callback(hObject, eventdata, handles)
% hObject handle to plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Plot using the popup menu
plot(handles.current_data);
function gsh_value_Callback(hObject, eventdata, handles)
% hObject handle to gsh_value (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 gsh_value as text
% str2double(get(hObject,'String')) returns contents of gsh_value as a double
% Convert string to numeric value for gsh_value
gsh_ivalue = str2double(get(hObject,'string'));
if isnan(gsh_ivalue)
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
% --- Executes during object creation, after setting all properties.
function gsh_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to gsh_value (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 gssg_value_Callback(hObject, eventdata, handles)
% hObject handle to gssg_value (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 gssg_value as text
% str2double(get(hObject,'String')) returns contents of gssg_value as a double
gssg_ivalue = str2double(get(hObject,'string'));
if isnan(gssg_ivalue)
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
% --- Executes during object creation, after setting all properties.
function gssg_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to gssg_value (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 glc_value_Callback(hObject, eventdata, handles)
% hObject handle to glc_value (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 glc_value as text
% str2double(get(hObject,'String')) returns contents of glc_value as a double
glc_ivalue = str2double(get(hObject,'string'));
if isnan(glc_ivalue)
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
% --- Executes during object creation, after setting all properties.
function glc_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to glc_value (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 button press in calculate_button.
function calculate_button_Callback(hObject, eventdata, handles)
% hObject handle to calculate_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Calculate the ode
[t,y]=ode45('project',[0 10],[glc_ivalue gsh_ivalue gssg_ivalue]);
Solution