Alon Shmiel
Alon Shmiel

Reputation: 7121

Matlab display animated gif in gui

I am trying to display a GIF image in my GUI, and it doesn't work. It displays me a fake image (not a GIF, and with different colors).

I know there is an "Animated GIF" in File Exchange but I prefer something else :/
I tried the next code, but it doesn't work:

function [] = GUI_400()

     hFig = figure('Name','Simulation Plot Window','Menubar','none', 'Resize','off', 'WindowStyle','modal', 'Position',[300 300 1150 600]);
     movegui(hFig, 'center');

     hAxes = axes('Parent',hFig,'Units','pixels','Position',[362 242 424 359]);  %#   so the position is easy to define
     image(imread('loading.gif', 'gif'),'Parent',hAxes);  %# Plot the image
     set(hAxes,'Visible','off', 'handlevisibility', 'off');          %# Turn the axes visibility off

end

this is my gif image: http://desmond.imageshack.us/Himg822/scaled.php?server=822&filename=loadingoz.gif&res=landing

Thank you!

Upvotes: 1

Views: 7448

Answers (2)

dsgrnt
dsgrnt

Reputation: 393

I would recommend displaying your gif like this. This way, you don't have a nasty while loop blocking callbacks and other code execution.

jLabel = javaObjectEDT('javax.swing.JLabel',javaObjectEDT('javax.swing.ImageIcon',which([fname '.gif'])));
[hJ,hC] = javacomponent(jLabel,getpixelposition(hFig).*[0 0 1 1],hFig);
set(hC,'units','normalized','position',[0 0 1 1])

Upvotes: 0

Amro
Amro

Reputation: 124563

Here is an example of a GIF player:

function gifPlayerGUI(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);

    %# prepare GUI, and show first frame
    hFig = figure('Menubar','none', 'Resize','off', ...
        'Units','pixels', 'Position',[300 300 imgW imgH]);
    movegui(hFig,'center')
    hAx = axes('Parent',hFig, ...
        'Units','pixels', 'Position',[1 1 imgW imgH]);
    hImg = imshow(img(:,:,:,1), map, 'Parent',hAx);
    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))

        %# pause for the specified delay
        pause(delay)
    end
end

EDIT

As I mentioned in the comments, the sample GIF image you posted is rather strange. Here are the changes to make it work. Inside the while loop, add the following immediately after the set(hImg,'CData',..) line:

%# update colormap
n = max(max( img(:,:,:,counter) ));
colormap( info(counter).ColorTable(1:n,:) )

Upvotes: 4

Related Questions