Matlab makes the loops run simultaneously

后端 未结 2 592
北海茫月
北海茫月 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.

    0 讨论(0)
  • 2021-01-27 02:14

    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

    0 讨论(0)
提交回复
热议问题