MATLAB: GUI Plot that has an interactive slider to change input parameter while running constantly

guislider

I am trying to create a GUI that runs nonstop (either for a large time 't' or infinite) to plot an equation while being able to use a slider to change the input parameter to adjust the graph as it plots the equation on the go. Here is what I have done:
I created a GUI plot using GUIDE that uses timer object as the time variable in the x-axis. It can run continuously nonstop and will plot the function.
Now my question is:
How do I implement a slider that can change the input parameters of the equation (as if I am turning a dial or a knob to increase/decrease volume)? The slider also has to update the plot instantly while it is being dragged back and forth (instead of just updating when releasing the left mouse button).
As of now my plot is running on a constant input (denoted as Pin in the code for input pressure)
Below is the code for my GUI. I attached the GUI .fig and .m files for convenience.
% code




function varargout = single_chamber_pressure(varargin)
% SINGLE_CHAMBER_PRESSURE MATLAB code for single_chamber_pressure.fig
% SINGLE_CHAMBER_PRESSURE, by itself, creates a new SINGLE_CHAMBER_PRESSURE or raises the existing
% singleton*.
%




% H = SINGLE_CHAMBER_PRESSURE returns the handle to a new SINGLE_CHAMBER_PRESSURE or the handle to
% the existing singleton*.
%
% SINGLE_CHAMBER_PRESSURE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SINGLE_CHAMBER_PRESSURE.M with the given input arguments.
%
% SINGLE_CHAMBER_PRESSURE('Property','Value',...) creates a new SINGLE_CHAMBER_PRESSURE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before single_chamber_pressure_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to single_chamber_pressure_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 single_chamber_pressure
% Last Modified by GUIDE v2.5 06-Apr-2016 02:07:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @single_chamber_pressure_OpeningFcn, ...
'gui_OutputFcn', @single_chamber_pressure_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
% code
% --- Executes just before single_chamber_pressure is made visible.
function single_chamber_pressure_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 single_chamber_pressure (see VARARGIN)
% Choose default command line output for single_chamber_pressure
handles.output = hObject;
handles.timer=timer('TimerFcn',{@user_timercallback,hObject},...
'ExecutionMode','fixedSpacing',...
'Period',0.01); %hObject is the arg for timer callback function
handles.t=0;
handles.signal=line('parent',handles.axes_myax,...
'Xdata',[],...
'Ydata',[],...
'color',[0 0 1],...
'EraseMode','background');
handles.dot=line('parent',handles.axes_myax,...
'Xdata',[],...
'Ydata',[],...
'marker','o',...
'markeredgecolor','r',...
'EraseMode','background');
set(handles.axes_myax,'ylim',[0 2]);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes single_chamber_pressure wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% code
% --- Outputs from this function are returned to the command line.
function varargout =single_chamber_pressure_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;
% code
% --- Executes on button press in pushbutton_start.
function pushbutton_start_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
runstr=get(hObject,'string');
if strcmpi(runstr,'START')
set(hObject,'string','STOP');
set(hObject,'backgroundcolor','r');
start(handles.timer);
else
set(hObject,'string','START');
set(hObject,'backgroundcolor','g');
stop(handles.timer);
end
guidata(hObject,handles);
% code
% The equation is calculated and then plotted in the function below
function user_timercallback(obj,event,hObject)
handles=guidata(hObject);
handles.t=handles.t+0.01;
t=0:0.01:handles.t;
% Initial Conditions
Pin = 3; % Input Pressure - Controlled by User
P1 = 1; % Initial Pressure 1
r0 = 1; % Initial chamber radius
dP1_shutoff = 1.2; % Shutoff Pressure
R = 1; % Flow Resistance
% Calculation of Pressure 1
if -dP1_shutoff < Pin - P1
Vdot = (Pin - P1)./R;
else
Vdot = 0;
end
V1 = Vdot*t; % Updated volume of the chamber
r = ((3./(4.*pi)).*V1).^(1./3); % Updated radius of the chamber
P1 = 2*[1./(r0.^2.*r)].*[1-(r0./r).^6]; % Updated Pressure 1
x = t;
y = P1;
set(handles.axes_myax,'xlim',[0 handles.t]);
set(handles.signal,'xdata',t,'ydata',y);
set(handles.dot,'xdata',t(end),'ydata',y(end));
guidata(hObject,handles);

Best Answer

You can add a listener to the Value of the slider to trigger a callback while the slider is being dragged rather than just when it is released.
addlistener(my_slider,'Value','PreSet',@my_callback)