Matlab makes the loops run simultaneously

后端 未结 2 591
北海茫月
北海茫月 2021-01-27 01:44

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

2条回答
  •  孤街浪徒
    2021-01-27 01:53

    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.

提交回复
热议问题