Matlab function to GUI - gif animation transfer (Timerfcn error)

1 view (last 30 days)
Hello everyone,
This was the code that I used for displaying my gif image 'pacman.gif'
function gifplayer
% The function displays any animated GIF's in a figure window
[pathstr, name, ext] = fileparts('pacman.gif'); % file information retrieved here
[handles.im,handles.map] = imread('pacman.gif','frames','all'); %read all frames of an gif image
handles.len = size(handles.im,4); % number of frames in the gif image
handles.h1 = imshow(handles.im(:,:,:,1),handles.map);%loads the first image along with its colormap
handles.guifig = gcf;
handles.count = 1;% intialise counter to update the next frame
handles.tmr = timer('TimerFcn', {@TmrFcn,handles.guifig},'BusyMode','Queue',...
'ExecutionMode','FixedRate','Period',0.09); %form a Timer Object
guidata(handles.guifig, handles);
start(handles.tmr); %starts Timer
set(gcf,'CloseRequestFcn',{@CloseFigure,handles});
function TmrFcn(src,event,handles)
%Timer Function to animate the GIF
handles = guidata(handles);
set(handles.h1,'CData',handles.im(:,:,:,handles.count)); %update the frame in the axis
handles.count = handles.count + 1; %increment to next frame
if handles.count > handles.len %if the last frame is achieved intialise to first frame
handles.count = 1;
end
guidata(handles.guifig, handles);
function CloseFigure(src,event,handles)
% Function CloseFigure(varargin)
stop(handles.tmr);delete(handles.tmr);%removes the timer from memory
closereq;
But when I tried to load this in my GUI mainframe as
axes(handles.Pacmanpic)
gifplayer
I am getting the following error message:
Error while evaluating TimerFcn for timer 'timer-6'
Reference to non-existent field 'h1'
What am I doing wrong? I am fairly new to GUI, so please help me out.
Thank you!
  1 Comment
Geoff Hayes
Geoff Hayes on 15 Mar 2015
Ara - I ran a simplified version of the above code and there was no problem in trying to reference the field h1. I suggest that you put a couple of breakpoints in the code - the first at
handles.tmr = timer(...
so that you can verify that handles does indeed have the field h1 initialized. The second breakpoint would go at the first line of the timer function so that you can step over this line and see what data members/fields are a part of handles. Note that your function signature should changes as the third input is not a variable called handles but is the handle to the GUI/figure. To avoid confusion, please change this to
function TmrFcn(src,event,hGui)
%Timer Function to animate the GIF
handles = guidata(hGui);
Please try the above and report your results.

Sign in to comment.

Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!