My matlab while loop isn't recognising my function button press - matlab

I have a script that runs when a button is pressed causing a while loop to initiate. When my stop button is pressed it is supposed to cause the loop condition to be untrue thus stopping the loop. My stop button doesn't seem to stop the loop during testing. it would seem the value I assigned in the pushDisconnect button isn't updating to the while loop and i'm not sure why this is occurring.
% --- Executes on button press in pushConnect.
function pushConnect_Callback(hObject, eventdata, handles)
% hObject handle to pushConnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushDisconnect,'Enable','on')
set(handles.pushConnect,'Enable','off')
handles.myDevice.StartAcquisition;
sampleRate_BP = double(handles.myDevice.BioPotentialSignals.SamplesPerSecond);
axis_handles = zeros(1,handles.numEnabledBPChannels);
BioPotentialSignals = cell(1,handles.numEnabledBPChannels);
handles.CheckFinger = cell(1,5);
handles.stopNow = 0;
for ch = 1:handles.numEnabledBPChannels
axis_handles(ch) = subplot(length(axis_handles),1,ch,'Parent',handles.uipanelGraph);
if ch==1
title(char(handles.deviceName))
end
ylabel([char(handles.myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
hold on
end
xlabel('Time (s)')
linkaxes(axis_handles,'x')
plotWindow = 5;
plotGain_BP = 1;
while handles.stopNow == 0
for ch = 1:handles.numEnabledBPChannels
BioPotentialSignals{ch} = [BioPotentialSignals{ch};handles.myDevice.BioPotentialSignals.Item(ch-1).GetScaledValueArray.double'];
if length(BioPotentialSignals{ch}) <= plotWindow*sampleRate_BP
cla(axis_handles(ch))
t = (0:(length(BioPotentialSignals{ch})-1))*(1/sampleRate_BP);
plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch});
xlim([0 plotWindow])
else
if ch==1
t = ((length(BioPotentialSignals{ch})-(plotWindow*sampleRate_BP-1)):length(BioPotentialSignals{ch}))*(1/sampleRate_BP);
end
cla(axis_handles(ch))
plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch}(end-plotWindow*sampleRate_BP+1:end));
xlim([t(end)-plotWindow t(end)])
end
end
% Update handles structure
guidata(hObject, handles);
pause(0.1)
disp(handles.stopNow)
end
% Stop signal
handles.myDevice.StopAcquisition;
% Disconnect from all sensors
handles.myDevice.Disconnect;
msgbox('Device requires cycling, in order to restablish connection. Closing program.')
% Close window
close all;
% --- Executes on button press in pushDisconnect.
function pushDisconnect_Callback(hObject, eventdata, handles)
% hObject handle to pushDisconnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushDisconnect,'Enable','off')
handles.stopNow = 1;
% Update handles structure
guidata(hObject, handles);
Final Working Code, if anyone is interested:
% --- Executes on button press in pushConnect.
function pushConnect_Callback(hObject, eventdata, handles)
% hObject handle to pushConnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushDisconnect,'Enable','on')
set(handles.pushConnect,'Enable','off')
handles.myDevice.StartAcquisition;
sampleRate_BP = double(handles.myDevice.BioPotentialSignals.SamplesPerSecond);
axis_handles = zeros(1,handles.numEnabledBPChannels);
BioPotentialSignals = cell(1,handles.numEnabledBPChannels);
handles.CheckFinger = cell(1,5);
handles.stopNow = 0;
for ch = 1:handles.numEnabledBPChannels
axis_handles(ch) = subplot(length(axis_handles),1,ch,'Parent',handles.uipanelGraph);
if ch==1
title(char(handles.deviceName))
end
ylabel([char(handles.myDevice.BioPotentialSignals.Item(ch-1).Name) ' (V)']);
hold on
end
xlabel('Time (s)')
linkaxes(axis_handles,'x')
plotWindow = 5;
plotGain_BP = 1;
% Update handles structure
guidata(hObject, handles);
while handles.stopNow == 0
for ch = 1:handles.numEnabledBPChannels
BioPotentialSignals{ch} = [BioPotentialSignals{ch};handles.myDevice.BioPotentialSignals.Item(ch-1).GetScaledValueArray.double'];
if length(BioPotentialSignals{ch}) <= plotWindow*sampleRate_BP
cla(axis_handles(ch))
t = (0:(length(BioPotentialSignals{ch})-1))*(1/sampleRate_BP);
plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch});
xlim([0 plotWindow])
else
if ch==1
t = ((length(BioPotentialSignals{ch})-(plotWindow*sampleRate_BP-1)):length(BioPotentialSignals{ch}))*(1/sampleRate_BP);
end
cla(axis_handles(ch))
plot(axis_handles(ch),t,plotGain_BP*BioPotentialSignals{ch}(end-plotWindow*sampleRate_BP+1:end));
xlim([t(end)-plotWindow t(end)])
end
end
handles = guidata(hObject);
drawnow
pause(0.1)
end
% Stop signal
handles.myDevice.StopAcquisition;
% Disconnect from all sensors
handles.myDevice.Disconnect;
msgbox('Device requires cycling, in order to restablish connection. Closing program.')
% Close window
close all;
% --- Executes on button press in pushDisconnect.
function pushDisconnect_Callback(hObject, eventdata, handles)
% hObject handle to pushDisconnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushDisconnect,'Enable','off')
handles.stopNow = 1;
% Update handles structure
guidata(hObject, handles);

Kill the guidata(hObject, handles) line, and replace it by handles=guidata(hObject) since you're overwriting the GUI state with whatever was current at the time the loop started, and you want to be polling the GUI state instead. Put the guidata(hObject, handles)-line before the loop, to ensure that handles.stopNow is indeed reset to 0.
If that doesn't help, an additional problem may be that Matlab waits with processing commands from one button callback until the other callback is done running.
Two things you can try: Add a drawnow command before the pause. This will force the GUI to update, and processes all the interactions. Also, make sure that the pushbutton callback for pushConnect is interruptible.

Related

Passing values from radio buttons into the script in Matlab

So i created GUI in guide that looks like this:
GUI
I want to access data from radio button and then change the variables in my simulation (Bitrate and Modulation are the button groups, Improvement is a single radio button). For example - in the simulation I have a variable Rs=1e9, so when 1Gbps button is selected I want it to remain 1e9, but if 10Gbps button is selected I want it to change its value to 10e9.
Then after hitting Start button I want to start my simulation (which is in different .m file) with given parameters. How can I do it ? (I know about handles idea in matlab, but I don't know how to pass value to the simulation)
That's the code that controls gui - generated by guide. I added some code that starts simulation and close gui window.
function varargout = gui_final(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #gui_final_OpeningFcn, ...
'gui_OutputFcn', #gui_final_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 gui_final is made visible.
function gui_final_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)
% Choose default command line output for gui_final
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_final_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 start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation');
% --- Executes on button press in dfe.
function dfe_Callback(hObject, eventdata, handles)
% hObject handle to dfe (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 dfe
% --- Executes on button press in ook.
function ook_Callback(hObject, eventdata, handles)
% hObject handle to ook (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 ook
You can do it using the object "hObject"
Like this: Suppose that you have an slider, everytime the slider is moved the callback is called. You can use it to store a variable. Then, when you press a button you want to use that data for whatever. The code is:
function slider_callback(hObject,eventdata)
sval = hObject.Value;
diffMax = hObject.Max - sval;
data = struct('val',sval,'diffMax',diffMax);
hObject.UserData = data;
% For R2014a and earlier:
% sval = get(hObject,'Value');
% maxval = get(hObject,'Max');
% diffMax = maxval - sval;
% data = struct('val',sval,'diffMax',diffMax);
% set(hObject,'UserData',data);
end
function button_callback(hObject,eventdata)
h = findobj('Tag','slider1');
data = h.UserData;
% For R2014a and earlier:
% data = get(h,'UserData');
display([data.val data.diffMax]);
end
REFERENCE: Matlab Docs
UPDATE
In your case you can do it with another approach if you want. When you press the button START you read the state of your radio buttons, and pass the appropiate value to your simulation_function that I suppose it is called "simulation". In the following example I suppose that you have a button-group called (tag): uibuttongroup1, and inside you have two buttons: radiobutton1 and radiobutton2
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Check which radio button is pressed
switch get(get(handles.uibuttongroup1,'SelectedObject'),'Tag')
case 'radiobutton1', myParameter = 1e9;
case 'radiobutton2', myParameter = 10e9;
end
%Execute simulation
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation(myParameter)');

Make the slider as a progress bar in matlab GUIDE

I am having hard time to make slider to behave as a progress bar.
I have two push buttons. One accepts the starting value from a text editor(User Input) and other start the progress of the loop.
Then I have got a slider, which I am trying to move as the function loop changes 'i' value(Update along/show progress).
This is my code.
function varargout = myfig(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #myfig_OpeningFcn, ...
'gui_OutputFcn', #myfig_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 myfig is made visible.
function myfig_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 myfig (see VARARGIN)
% Choose default command line output for myfig
handles.output = hObject;
handles.min = get(handles.ed,'value');
handles.max = 10000;
handles.i = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myfig wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myfig_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 pb.
function pb_Callback(hObject, eventdata, handles)
% hObject handle to pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value = str2double(get(handles.ed,'string'));
value
assignin('base','value',value)
% --- Executes on slider movement.
function sl_Callback(hObject, eventdata, handles)
% hObject handle to sl (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.sl,'Max',handles.max);
set(handles.sl,'min',handles.min);
set(handles.sl,'value',handles.i);
% --- Executes during object creation, after setting all properties.
function sl_CreateFcn(hObject, eventdata, handles)
% hObject handle to sl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function ed_Callback(hObject, eventdata, handles)
% hObject handle to ed (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 ed as text
% str2double(get(hObject,'String')) returns contents of ed as a double
% --- Executes during object creation, after setting all properties.
function ed_CreateFcn(hObject, eventdata, handles)
% hObject handle to ed (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 pbs.
function pbs_Callback(hObject, eventdata, handles)
% hObject handle to pbs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myfile;
function [] = myfile(handles)
%MYFILE Summary of this function goes here
% Detailed explanation goes here
% prompt('Please enter a valid number to start');
h = waitbar(0,'Processing...');
% i = input('Please enter a valid number to start');
i = evalin('base','value');
while(i < 10000)
clc
i = i + 1;
waitbar(i/10000,h)
handles.i = i;
set(handles.sl,'value',i/10000);
guidata(hObject,handles);
end
close(h);
Matlab verstion: 2014b
First: I want to learn how can I handle this when function is nested inside the GUI file.
Second: If you move the function into a different .m file.[Using set/get/guidata etc... and not passing input/output with function call]
Please let me know.
For the first case, you will need to modify your function myfile. Before the while loop, you'll need to set the min and max of the slider. This will allow you to use i as your value.
set(handles.sl,'Min',1,'Max',10000);
To allow the slider to update, you will need to add refresh and drawnow inside your loop. Matlab will wait to draw ui changes until the end of your code if you so not use these. Your loop should look like the following:
while(i < 10000)
clc
i = i + 1;
waitbar(i/10000,h)
set(handles.sl,'value',i/10000);
refresh;
drawnow;
end
From personal experience, the second case can get messy for larger applications. I would recommend creating your application using object oriented programming if you need to use more than one file. You could store ui handles in the objects you create, which keeps your code organized and makes it easier to pass handles back and forth.

Matlab GUI/GUIDE Appending item to vector stored in in a handle

I'm working on a program that records user button-presses in response to a sequence of tones. I am storing the timing of the button presses in a vector (pushTimes), which I am storing in GUIDE's "handles" structure, and I would like the new time of each button press to be appended to the pushTimes vector. However, handles doesn't seem to be storing the new vector with the appended value, leaving me with an empty vector after the button is pressed.
Edit: Because people were having difficulty reproducing the error, I'm posting the whole file The relevant code is in the callBack pushButton1. pushButton2 and pushButton3 are "pause" and "start" buttons, respectively. The error occurs even when I don't pause the program.
function varargout = simpleGui(varargin)
% SIMPLEGUI M-file for simpleGui.fig
% SIMPLEGUI, by itself, creates a new SIMPLEGUI or raises the existing
% singleton*.
%
% H = SIMPLEGUI returns the handle to a new SIMPLEGUI or the handle to
% the existing singleton*.
%
% SIMPLEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SIMPLEGUI.M with the given input arguments.
%
% SIMPLEGUI('Property','Value',...) creates a new SIMPLEGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before simpleGui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to simpleGui_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 simpleGui
% Last Modified by GUIDE v2.5 27-May-2014 11:00:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #simpleGui_OpeningFcn, ...
'gui_OutputFcn', #simpleGui_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 simpleGui is made visible.
function simpleGui_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 simpleGui (see VARARGIN)
[soundVec, codeVec] = FourToneDifDirection();
handles.soundVec = soundVec;
handles.codeVec = codeVec;
handles.toneTimes = zeros(1,length(soundVec));
handles.pushTimes = [];
handles.toneNum = 1;
% Choose default command line output for simpleGui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes simpleGui wait for user response (see UIRESUME)
% --- Outputs from this function are returned to the command line.
function varargout = simpleGui_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 handles = pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dv = datevec(now);
%get the seconds from datevec
sec = dv(6);
%append to the pushTimes vector
handles.pushTimes = [handles.pushTimes sec]
%debugging line, should return a large vector after button is pressed multiple times but only returns a single value.
handles.pushTimes
guidata(hObject, handles)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
UIresume
guidata(hObject, handles);
soundVec = handles.soundVec
n = 1;
while(n<length(soundVec))
guidata(hObject, handles);
n = handles.toneNum
midigen(handles.soundVec(n), 0.25);
dv = datevec(now);
sec = dv(6);
handles.toneTimes(n) = sec;
n = n+1;
handles.toneNum = n;
end
guidata(hObject, handles)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
codeVec = handles.codeVec;
toneTimes = handles.toneTimes;
pushTimes = handles.pushTimes
calculateHitRate(codeVec, toneTimes, pushTimes,1,handles.toneNum);
guidata(hObject, handles)
uiwait(handles.figure1);
function out = isWithin(x, low, high)
out = x > low & x < high;
function [d1hr, d2hr, d3hr, d4hr] = calculateHitRate(codeVec, toneTimes, pushTimes, start, finish)
d1Ct = sum(codeVec(start:finish) == 9);
d2Ct = sum(codeVec(start:finish) == 10);
d3Ct = sum(codeVec(start:finish) == 11);
d4Ct = sum(codeVec(start:finish) == 12);
d1Push = 0;
d2Push = 0;
d3Push = 0;
d4Push = 0;
for i = 1:length(pushTimes)
pushTime = pushTimes(i)
stimTime = pushTime - 2;
for j = start:finish
%if the stimulus was within 1.5 seconds before the push
if codeVec(j) == 9 && isWithin(toneTimes(j), stimTime, pushTime)
d1Push = d1Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d2Push = d2Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d3Push = d3Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d4Push = d4Push+1;
end
end
end
d1hr = d1Push/d1Ct
d2hr = d2Push/d2Ct
d3hr = d3Push/d3Ct
d4hr = d4Push/d4Ct

Updating GUI handles when using callback

I have a GUI made from GUIDE and I cannot figure out how to update a GUI handle when I call a callback in a callback. So for instance in the function which calls the function all I have is the following:
function start_ss_Callback(hObject, eventdata, handles)
% hObject handle to start_ss (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 start_ss as text
% str2double(get(hObject,'String')) returns contents of start_ss as a double
start_hh_Callback(hObject, eventdata, handles)
and in start_hh_Callback I have the code given below but my handles.plot_holds doesn't update regardless of the fact that I have guidata(hObject, handles). Is this because I'm using it as a function? If I just go through the start_hh_Callback itself not through start_ss_Callback, it updates. However, if I use it within a callback like start_ss_Callback it does not update.
I hope my question is clear, let me know if you need clarification.
function start_hh_Callback(hObject, eventdata, handles)
% hObject handle to start_hh (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 start_hh as text
% str2double(get(hObject,'String')) returns contents of start_hh as a double
axes(handles.axes1)
if isempty(handles.plot_holds)
cla
plot_button_Callback(hObject, eventdata, handles)
else
% in the event the time is changed after more than 1 plot is held then
% the additional plot times will be re evaluated for each parameter and
% the new indicies will take into affect.
myParams = get(handles.load_params_button,'string');
mystartDD = str2num(get(handles.start_dd,'string'));
mystartHH = str2num(get(handles.start_hh,'string'));
mystartMM = str2num(get(handles.start_mm,'string'));
mystartSS = str2num(get(handles.start_ss,'string'));
myendDD = str2num(get(handles.end_dd,'string'));
myendHH = str2num(get(handles.end_hh,'string'));
myendMM = str2num(get(handles.end_mm,'string'));
myendSS = str2num(get(handles.end_ss,'string'));
startFromStart = (mystartDD)*60*60*24 + (mystartHH-handles.startHour)*60*60 ...
+ (mystartMM-handles.startMinute)*60 + (mystartSS-handles.startSecond);
endFromStart = (myendDD)*60*60*24 + (myendHH-handles.startHour)*60*60 ...
+ (myendMM-handles.startMinute)*60 + (myendSS-handles.startSecond);
iStart = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time > (startFromStart+handles.startTimeOffset),1);
iEnd = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time > (endFromStart+handles.startTimeOffset),1);
if isempty(iEnd)
iEnd = length(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time);
end
cla
hold off
plot(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time(iStart:iEnd),...
handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Data(iStart:iEnd))
grid minor
box on
handles.plot_holds(1,3) = iStart;
handles.plot_holds(1,4) = iEnd;
if size(handles.plot_holds,1)>1
hold all
for ii = 2:size(handles.plot_holds)
iStart = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time > (startFromStart+handles.startTimeOffset),1);
iEnd = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time > (endFromStart+handles.startTimeOffset),1);
if isempty(iEnd)
iEnd = length(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time);
end
plot(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time(iStart:iEnd),...
handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Data(iStart:iEnd))
handles.plot_holds(ii,3) = iStart;
handles.plot_holds(ii,4) = iEnd;
end
end
end
legend(handles.myLegends{1:length(handles.myLegends)})
guidata(hObject, handles);
You'll have to load the handles again, after any handles-modifying function:
% this modifies and writes the handles to the guidata
start_hh_Callback(hObject, eventdata, handles);
% now read back the updated value
handles = guidata(hObject);
Alternatively you could also make handles the return value of you callback.
Usually this will be ignored when it's really used as a callback and when used as a "normal" function it avoids the need to re-read from guidata.
Your code could then look like this:
handles = start_hh_Callback(hObject, eventdata, handles);
with your callback redefined to:
function [handles] = start_hh_Callback(hObject, eventdata, handles)
...
end

How to create a GUI to play, pause, fast forward and rewind video in MATLAB?

I am a newbie to MATLAB. I am trying to create a GUI to play, pause, fast forward and rewind an avi video frame by frame. At the moment I can play and pause the video, via a toggle button, but when I press play again the video plays from frame zero. I realise I need to store the frame number to be used the next time I press play but I don't know how to do this.
Any help would be much appreciated. I realise there is an implay option in MATLAB but I have some other code to implement which I have already got right and that is why I want to create my own GUI. Below is the code to pause/play the video.
My most recent code looks like,
function varargout = N_Play_Pause_2(varargin)
% N_PLAY_PAUSE_2 MATLAB code for N_Play_Pause_2.fig
% N_PLAY_PAUSE_2, by itself, creates a new N_PLAY_PAUSE_2 or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE_2 returns the handle to a new N_PLAY_PAUSE_2 or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE_2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE_2.M with the given input arguments.
%
% N_PLAY_PAUSE_2('Property','Value',...) creates a new N_PLAY_PAUSE_2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause_2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause_2_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 N_Play_Pause_2
% Last Modified by GUIDE v2.5 29-Aug-2013 08:39:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause_2_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause_2_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 N_Play_Pause_2 is made visible.
function N_Play_Pause_2_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 N_Play_Pause_2 (see VARARGIN)
% Choose default command line output for N_Play_Pause_2
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; %Current video position. Starts at 1.
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes N_Play_Pause_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_2_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 togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (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 togglebutton1
while get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos+1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos-1; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos+10; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
% --- 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)
if get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
handles.videoPos=handles.videoPos-10; % Increment the stored position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
set(hObject,'Value',false);
Final solution
So, I haven't seen that you updated your code. Seeing your code is quite easier, it seems that you are incrementing after you show your image, so if you push the play button it will show the image before
function varargout = N_Play_Pause2(varargin)
% N_PLAY_PAUSE2 MATLAB code for N_Play_Pause2.fig
% N_PLAY_PAUSE2, by itself, creates a new N_PLAY_PAUSE2 or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE2 returns the handle to a new N_PLAY_PAUSE2 or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE2.M with the given input arguments.
%
% N_PLAY_PAUSE2('Property','Value',...) creates a new N_PLAY_PAUSE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause2_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 N_Play_Pause2
% Last Modified by GUIDE v2.5 23-Aug-2013 13:50:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause2_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause2_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 N_Play_Pause2 is made visible.
function N_Play_Pause2_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 N_Play_Pause2 (see VARARGIN)
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi');
handles.nFrames = handles.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
% Choose default command line output for N_Play_Pause2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes N_Play_Pause2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause2_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 togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (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 togglebutton1
while get(hObject,'Value') && handles.videoPos < handles.nFrames
handles.videoPos=handles.videoPos+1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
handles.videoPos=handles.videoPos+1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos>1
handles.videoPos=handles.videoPos-1; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject,'Value') && handles.videoPos<handles.nFrames-9
handles.videoPos=handles.videoPos+10; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
% --- 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)
if get(hObject,'Value') && handles.videoPos>10
handles.videoPos=handles.videoPos-10; % Increment the stored position
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(handles.videoPos);
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Seems you got confused a little bit with the coding, I've edited it and commented your errors.
They are preceded by % COMMENT:
function varargout = N_Play_Pause(varargin)
% N_PLAY_PAUSE MATLAB code for N_Play_Pause.fig
% N_PLAY_PAUSE, by itself, creates a new N_PLAY_PAUSE or raises the existing
% singleton*.
%
% H = N_PLAY_PAUSE returns the handle to a new N_PLAY_PAUSE or the handle to
% the existing singleton*.
%
% N_PLAY_PAUSE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in N_PLAY_PAUSE.M with the given input arguments.
%
% N_PLAY_PAUSE('Property','Value',...) creates a new N_PLAY_PAUSE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before N_Play_Pause_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to N_Play_Pause_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 N_Play_Pause
% Last Modified by GUIDE v2.5 13-Aug-2013 16:26:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #N_Play_Pause_OpeningFcn, ...
'gui_OutputFcn', #N_Play_Pause_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 N_Play_Pause is made visible.
function N_Play_Pause_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 N_Play_Pause (see VARARGIN)
% Choose default command line output for N_Play_Pause
handles.output = hObject;
handles.VidObj = VideoReader('x05.avi'); % COMMENT: PAY ATTENTION, 's' was missing!
handles.nFrames = handle.VidObj.NumberOfFrames;
handles.videoPos = 1; % Current video position, starts at first frame.
% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.
% COMMENT: In the original code here, you would save guidata twice, you didn't need that, just save guidata after you make ALL ALTERATIONS IN IT!
% --- Outputs from this function are returned to the command line.
function varargout = N_Play_Pause_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 togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (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 togglebutton1
while get(hObject,'Value')
snapshot = read(handles.VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(double2str(handles.videoPos));
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Reading your question title I was like, what else you need? Do you also want a desert? But jokes a part, I was wrong, at least you have something working.
I am not an image specialist at matlab, but instead doing a = 0 in your callback function (and therefore reseting your video to start position), you will need to save your video position at your GUI. There are several ways of doing it, one way would be by using guidata, setappdata, or to pass it via arguments to your callbacks. You could call it a variable videoPos and add it to the handles struct that you store with guidata. Also save your nFrames variable in this struct.
The fast forward would be the same as you showed, but instead doing videoPos = videoPos + 1 you would do videoPos = videoPos + n, where n is the speed multiplier you want on the fast forward. To rewind, just decrement your videoPos, or reset it to 1, depending what you want.
Note: Don't forget to add checkers, you won't want your videoPos lesser than 0 or greater than nFrames.
On the function: N_Play_Pause_OpeningFcn Add the following data:
handle.VidObj = VideoReader('x05.avi');
handle.nFrames = VidObj.NumberOfFrames;
handle.videoPos = 1; % Current video position, starts at first frame.
% Update handles structure
guidata(hObject, handles); % Here you are saving the handles method at the hObject, this is the handle from your GUI figure.
Then, at your function togglebutton1_Callback do:
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (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 togglebutton1
% You won't need this line anymore a=0;
% If you would like to retrieve the stored guidata you would do it here, by doing **handles=guidata(hObject);** but this is not needed, the handles data stored at the figure handle is already given to you as the third argument internally by the matlab!
while get(hObject,'Value')
snapshot = read(VidObj,handles.videoPos); % Here we use the stored video position
imshow(snapshot),title(double2str(handles.videoPos));
if handles.videoPos >= handles.nFrames % Protect your code not to go be number of frames available
break;
end
handles.videoPos=handles.videoPos+1; % Increment the stored position
end
guidata(hObject,handles) % Save the modifications done at the handles structure at the figure handle
Note that you need to update the guidata everytime you quit your methods, so that you keep it updated and saved on the figure handle. One more detail is that the object you pass for the guidata don't need to be the figure handle, but any object holden by it, as the play button you have created. As in the guidata help:
GUIDATA(H, DATA) stores the specified data in the figure's
application data.
H is a handle that identifies the figure - it can be the figure
itself, or any object contained in the figure.
Now just add more buttons and work with the togglebutton1 method, the fastforward would be the same, but using instead of +1, +n.