使用 gif 的 MATLAB 动画启动画面



我正在创建一个 MATLAB 可执行应用程序。在 MATLAB 中创建可执行文件时,它们为您提供了添加初始屏幕的选项。我已经用普通图像,png和jpg尝试过这个。但我想使用动画图像,例如加载 gif 图像。因为我的程序需要一段时间来编译和执行,所以我希望用户知道它正在加载,所以他们不会完全退出或不退出。我尝试将 gif 图像添加到我的初始屏幕,但它不起作用。它只是显示了一个静止的图像。有没有办法将动画启动画面或 gif 添加到我的可执行 MATLAB 应用程序中。

我认为你不能。但是您可以做的是在窗口中显示 GIF 并每 x 秒更新每一帧。下面是一个示例

% Read in your GIF file. Don't forget to read in the colour map as it is
% required for display.
[I, map]=imread('http://i.imgur.com/K9CLvNm.gif','Frames','all');
% Create a figure to hold your splashscreen
hfig=figure;
set(hfig,'Menubar', 'none');
set(hfig,'name','Please wait. Loading...','numbertitle','off');
% Set a timer to dynamically update the plot every 0.1 sec
t=timer('TimerFcn', {@timerCallbackFcn, hfig, I, map},'ExecutionMode','FixedRate','Period',0.1);
% Start the timer
start(t);
% Do your stuff here
for j=1:10
    pause(1);
end
% Clean-up
stop(t);
delete(t);
delete(hfig);

然后在名为 timerCallbackFcn.m 的文件中创建计时器更新函数

% This is the timer function to update the figure. 
% Save as timerCallbackFcn.m
function timerCallbackFcn(hTimer, eventData, hfig, I, map)
    figure(hfig);
    % i is the frame index
    persistent i;    
    if isempty(i), i=1; end
    % display the i-th frame in the GIF
    imshow(I(:,:,i),map);    
    % increment frame index i
    i=i+1;
    numframes=size(I,4);
    if (i>numframes), i=1; end
end

最新更新