how to display a gif in matlab gui
Show older comments
I add a function in OpeningFcn function in order to display a gif picture in my gui.the openingFcn function and the adding function as followes:
function PWR_Sim_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for PWR_Sim
handles.output = hObject;
gifPlayer_zgp(handles, 'PWR');
% Update handles structure
guidata(hObject, handles);
function gifPlayer_zgp(handles, fname)
%# read all GIF frames
info = imfinfo(fname, 'gif');
delay = ( info(1).DelayTime ) / 100;
[img,map] = imread(fname, 'gif', 'frames','all');
[imgH,imgW,~,numFrames] = size(img);
axes(handles.axes1);
hImg = imshow(img(:,:,:,1), map);
pause(delay)
%
%# loop over frames continuously
counter = 1;
while ishandle(hImg)
% # increment counter circularly
counter = rem(counter, numFrames) + 1;
% # update frame
set(hImg, 'CData',img(:,:,:,counter))
% # update colormap
n = max(max( img(:,:,:,counter) ));
colormap( info(counter).ColorTable(1:n,:) );
% # pause for the specified delay
pause(delay)
end
the gif picture can display in my gui, however, when i close the gui, there is a error in matlab command windows:
Error using guidata (line 89)
H must be the handle to a figure or figure descendent.
Error in PWR_Sim>PWR_Sim_OpeningFcn (line 74)
guidata(hObject, handles);
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in PWR_Sim (line 42)
gui_mainfcn(gui_State, varargin{:});
if i delete the while loop in gifPlayer_zgp function, there will no errors in the commond windows,however, the gif picture can't work. Dose anyone know how to correct the error?
Answers (1)
Walter Roberson
on 16 Apr 2013
0 votes
Your opening function calls gifPlayer_zgp but that routine infinite loops and does not return until the GUI is closed, so the instruction in the opening function to call guidata() is not reached until the GUI is closed.
You should rewrite your code in terms of a timer() call.
Categories
Find more on Printing and Saving 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!