1

残念ながら、ループが 2 つあります。そのため、私のコードは最初のループを実行し、それが終了したときにのみ 2 番目のループを実行します。

しかし、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

編集:OK、その場合は、最初のループの代わりにタイマーを試してください

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

これは、2 つのループを同時に実行する前の質問に関連しているようです (少なくともそのように見えます)。

@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

このコードは、多数の画像に対して長い処理ステップの実行をシミュレートすると同時に、ユーザーを楽しませるためにアニメーションを表示します。

コードをシンプルにするために、(タイマーを使用して) 色を継続的に更新するパッチを示しています。これは、「loading...」のアニメーション GIF イメージの略です。

スクリーンショット

于 2012-06-18T06:40:11.527 に答える