1

不幸的是,我有两个循环。这就是为什么我的代码使第一个循环运行并且只有当它完成时,它才使第二个循环运行。

但我希望 gui 同时显示数据:在 hAxes 和 loading1 中。

我怎样才能做到?

hFigure = figure('units','pixels','position',[300 300 424 470],'menubar','none',...
'name','start processing','numbertitle','off','resize','off');        

hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]); 

loading1 = uicontrol('style','text','unit','pix','position',[0 72 424 40],...
'backgroundcolor','r','fontsize',20);

%% shows the data on hAxes
for i = 5:100
    if mod(i,2) == 0
        set(hAxes,'Color','b');
    else
        set(hAxes,'Color','g');
    end
    drawnow;
end

%% shows the data on loading1
for i=1:200
    image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
    set(loading1,'string',image2);
    drawnow;
end

这段代码是彼得的:

    function test1

    hFigure = figure('units','pixels','position',[300 300 424 470],'menubar','none','name','start processing','numbertitle','off','resize','off'); 

    % Your other setup calls
    hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]); 

    loading1 = uicontrol('style','text','unit','pix','position',[0 72 424 40],'backgroundcolor','r','fontsize',20);

    c = 1;
    t = timer('TimerFcn', @color_change_fcn,'StartDelay',1.0);
    start(t);

    for i=1:200
        image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
        set(loading1,'string',image2);
        drawnow;
    end

    function color_change_fcn
        if mod(c,2) == 0
            set(hAxes,'Color','b');
        else
            set(hAxes,'Color','g');
        end
        drawnow;
        c = c + 1;
    end
end

它不起作用(不显示 hAxes)。我看到它没有使 color_change_fcn 运行(我试图在 color_change_fcn 函数的第一行写: disp('test') ,但它什么也没打印。

4

2 回答 2

1

这是你想要的吗?只需组合循环体。

for i=1:200
    if mod(i,2) == 0
        set(hAxes,'Color','b');
    else
        set(hAxes,'Color','g');
    end

    image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
    set(loading1,'string',image2);
    drawnow;
end

编辑:好的,在这种情况下,尝试一个计时器而不是第一个循环

function output = main_function

% Your other setup calls
hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]); 

c = 0;
t = timer('TimerFcn', @color_change_fcn, 'Period', 1.0);
start(t);

for i=1:200
    image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
    set(loading1,'string',image2);
    drawnow;
end

function color_change_fcn
    if mod(c,2) == 0
        set(hAxes,'Color','b');
    else
        set(hAxes,'Color','g');
    end
    drawnow;
    c = c + 1;
end
end

请记住,MATLAB 控制流本质上是单线程的,因此如果 MATLAB 正忙于在其他地方工作,这些回调将不会运行。

于 2012-06-16T18:48:23.190 回答
1

这似乎与您之前的问题有关,您希望两个循环同时运行(至少看起来是这样)。

基于@Peter的回答,考虑以下工作示例:

function timerDemo()
    %# prepare GUI
    hFig = figure('Menubar','none', 'Resize','off');
    axes('XLim',[0 1], 'YLim',[0 1], 'Visible','off', ...
        'Units','normalized', 'Position',[0.1 0.2 0.8 0.6])
    hTxt = uicontrol('Style','text', 'FontSize',24, ...
        'Units','normalized', 'Position',[0 0.9 1 0.1]);
    hPatch = patch([0 0 1 1 0],[0 1 0 1 0],'k');

    %# colors to cycle through
    c = 1;
    clr = lines(4);

    %# create timer
    delay = 0.5;
    hTimer = timer('Period',delay, 'StartDelay',delay, ...
        'ExecutionMode','FixedRate', 'TimerFcn',@timerCallback);

    %# when figure is closed
    set(hFig, 'CloseRequestFcn',@onClose);

    %# process images
    start(hTimer);          %# start timer
    for i=1:100
        if ~ishandle(hFig), break; end

        msg = sprintf('Processing image %d / %d', i, 100);
        set(hTxt, 'String',msg)

        %# some lengthy operation
        pause(.1)
    end
    if isvalid(hTimer)
        stop(hTimer)        %# stop timer
        delete(hTimer)      %# delete timer
    end

    %# timer callback function
    function timerCallback(src,evt)
        if ~ishandle(hFig), return; end

        %# incremenet counter circularly
        c = rem(c,size(clr,1)) + 1;

        %# update color of patch
        set(hPatch, 'FaceColor',clr(c,:));
        drawnow
    end

    %# on figure close
    function onClose(src,evt)
        %# stop and delete timer
        if isvalid(hTimer)
            stop(hTimer);
            delete(hTimer);
        end

        %# call default close callback
        feval(@closereq)
    end
end

该代码模拟了对多个图像运行冗长的处理步骤,同时显示动画以保持用户的娱乐性。

为了保持代码简单,我展示了一个不断更新其颜色的补丁(使用计时器)。这代表“正在加载...”的动画 GIF 图像。

截屏

于 2012-06-18T06:40:11.527 回答