How to get a GifPlayer to work on GUI's?

3 views (last 30 days)
Jimmy
Jimmy on 22 Feb 2015
Answered: Geoff Hayes on 22 Feb 2015
When searching on the web for a gifplayer code to use when building GUI's, there's one that's provided as the "top-hit" http://www.mathworks.com/matlabcentral/fileexchange/12673-gif-player.
I've implemented the code into my GUI and cannot get it to work. What I mean is that I obviously change the file that I want the code to associate itself with but, the error that arises is "too many input arguments", as well as a separate figure window that pops up. Can anyone provide insight as to how to get the code to work? As well as keep the gif on a repeating loop?

Answers (1)

Geoff Hayes
Geoff Hayes on 22 Feb 2015
Jimmy - why not show your implementation of the code so that we can get an idea of what you have attempted and so provide some guidance on how to correct the problem?
If I create a very simple GUI (in GUIDE) with a pushbutton and an axes object (named pusbutton1 and axes1 respectively) so that the GifPlayer code (or a variation of it) is called when the button is pressed, then the button callback would be something like
function pushbutton1_Callback(hObject, eventdata, handles)
gifplayer(handles.figure1);
In this example, I will be using one of the default images that comes with the GifPlayer. Note how the figure handle is passed in to the gifplayer function. I'm doing this so that we can access the handles structure of the GUI. This requires that we change the gifplayer signature and first few lines of code to
function gifplayer(hFigure,gif_image,delay_length)
if nargin<2
gif_image = 'crystal.gif';
delay_length = 0.2;%frame will be updated after 0.2 sec
elseif nargin <3
delay_length = 0.2;%frame will be updated after 0.2 sec
end
% get the handles structure of the GUI
handles = guidata(hFigure);
[pathstr, name, ext] = fileparts(gif_image); % file information retrieved here
Depending upon your version of MATLAB, you may have to modify the above call to fileparts and remove the versn parameter which is no longer supported (at least not in my R2014a).
The next block of code now becomes
if strcmp(ext,'.gif')
[handles.im,handles.map] = imread(gif_image,'frames','all'); %read all frames of an gif image
handles.len = size(handles.im,4); % number of frames in the gif image
handles.h1 = image(handles.im(:,:,:,1));
colormap(handles.map);
handles.count = 1;% intialise counter to update the next frame
handles.tmr = timer('TimerFcn', {@TmrFcn,hFigure},'BusyMode','Queue',...
'ExecutionMode','FixedRate','Period',delay_length); %form a Timer Object
guidata(hFigure, handles);
start(handles.tmr); %starts Timer
else
error('Not a GIF image.Load only GIF images'); %If image is not GIF, show this error
end
Note the use of hFigure as an input parameter to the timer function and how it is used to update the handles structure. In the above, I am using image rather than imshow since I don't have the Image Processing Toolbox. The timer function now becomes
function TmrFcn(src,event,hFigure)
%Timer Function to animate the GIF
handles = guidata(hFigure);
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(hFigure, handles);
Again, note the use of hFigure to retrieve and then update the handles structure.
Try implementing the above into your GUI and see if it works as you expect.

Categories

Find more on Interactive Control and Callbacks 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!