Matlab makes the loops run simultaneously

我的梦境 提交于 2021-02-05 08:11:59

问题


Unfortunately, I have two loops. that's why my code makes the first loop run and only when it is finished, it makes the second loop run.

But I want the gui to show the data simultaneously: in the hAxes and in the loading1.

How can I make it?

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

this code is of Peter:

    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

It doesn't work (doesn't show the hAxes). I saw it doesn't make the color_change_fcn run (I tried to write: disp('test') in the first row of color_change_fcn function, but it prints nothing.


回答1:


This seems to be related to your previous question, where you want the two loops to be running simultaneously (well at least appear to be that way).

Building on @Peter's answer, consider the following working example:

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

The code simulates running a lengthy processing step on a number of images, while at the same time showing an animation to keep the user entertained.

To keep the code simple, I am showing a patch that updates its color continuously (using a timer). This stands for the animated GIF image of "loading...".

screenshot




回答2:


Is this what you want? Just combine the loop bodies.

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

EDIT: OK, in that case, try a timer instead of the first loop

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

Just remember that the MATLAB control flow is inherently single-threaded, so these callbacks won't run if MATLAB is busy working somewhere else.



来源:https://stackoverflow.com/questions/11065825/matlab-makes-the-loops-run-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!