Search code examples
matlabparallel-processingmatlab-figureparallels

Unable to run two function simultanously under App Designer MATLAB


Objective

The objective is to run two functions simultaneously using app that design using App designer. In general, there are 3 main blocks

1) App1. The apps contains 1 state button (e.g., STOP BUTTON) and 1 button (TASK).

2) Function FirstTask. Under the hood, datetime is sampled at every iteration.

3) Function SecondTask. Similarly, under the hood, datetime is sampled at every iteration.

** For reproduciabilty, we simplify the example of sampling datatime.

The procedure are as follow;

Function FirstTask is execute at the beginning/startup of App1.

Whereas, Function SecondTask only executed after the Button TASK being pressed.

Both Function FirstTask and Function SecondTask terminated simultaneously after STOP BUTTON being pressed.

Observations

There are three observations made;

1) Function FirstTask is execute as intended at the beginning/startup of App1.

2) Function SecondTask is execute as intended after the Button TASK being pressed.

3) The Function FirstTask is put to halt after Button TASK being pressed despite being independent of the Button TASK.

My question is, how to tackle the observation No 3. Because, we required both Function FirstTask and Function SecondTask to run simultaneously.

The code to reproduce the above problem are

1) Code at the app1.

methods (Access = private)
    % Code that executes after component creation
    function startupFcn(app)
        FirstTask(app)
    end
    % Button pushed function: RunSecondTaskButton
    function RunSecondTaskButtonPushed(app, event)
        SecondTask(app)
    end
end

2) Function FirstTask

function FirstTask(Gui)
initVar=1;
MaximumData=1000; % Maximum before we append further
FirstData=NaT(MaximumData,1); % Prelocate
while Gui.StopButton.Value==0  % Loop while button stop no click
    FirstData(initVar)=datetime('now','Format','HH:mm:ss.SSS');   % add the time vector duration for each day
    initVar=initVar+1;
    pause(1)
end
end

3) Function SecondTask

function SecondTask(Gui)
initVar=1;
MaximumData=1000; % Maximum before we append further
SecondData=NaT(MaximumData,1); % Prelocate
while Gui.StopButton.Value==0  % Loop while button stop no click
SecondData(initVar)=datetime('now','Format','HH:mm:ss.SSS');   % add the time vector duration for each day
    initVar=initVar+1;
    pause(1)
end
end

The full code is attach in this thread. Really appreciate for any advice on this matter.


Solution

  • Thanks to the suggestion. Using timer, the following can realised by something like.

            app.GraphTimer=timer;
            app.GraphTimer.TimerFcn = @app.FirstTaskx;
            app.GraphTimer.ExecutionMode  = 'fixedRate';
    

    In the function,within the app designer

        function FirstTaskx(app,~,~)
            FirstTask(app)
        end
    

    Repeat the same for the second task.