0

This question is related to This

While running the while loop, how can I keep listening or looking at pushbutton2, so that if there is a push I can perform some extra operations ?

Community
  • 1
  • 1
cppiscute
  • 707
  • 2
  • 10
  • 33
  • You should add calls to [`drawnow`](http://fr.mathworks.com/help/matlab/ref/drawnow.html) in your loop in order to let matlab process pending callbacks. – CitizenInsane Feb 12 '16 at 12:45
  • See my other post [here](http://stackoverflow.com/a/28378219/684399) for related issue. – CitizenInsane Feb 12 '16 at 12:48
  • @CitizenInsane...I strongly beleive drawnow is not at all important here. All I want to do is..........while the "while loop" is running I want to update the pushbutton status and based on the status I want to update some value in the while loop. So to sum up.....I just need to take the status change of the pushbutton from the GUI to the external function[Situated outside the GUI's .m file]. I am pretty sure this can be done with a single line of code. Let me check. – cppiscute Feb 12 '16 at 13:24

1 Answers1

2

Matlab is mono-threaded, that is when it is executing some code (i.e. your while loop) it cannot process any other events (i.e. your pushbutton) until code is finished.

Look at below simple example to demonstrate this:

%% --- GUI creation
function [] = mygui()
%[
    fig = figure(666);
    clf;
    uicontrol('Parent', fig, 'Units', 'Normalized', 'Position', [0.2 0.4 0.7 0.1], 'String', 'Start script', 'Callback', @onStartScript);
    uicontrol('Parent', fig, 'Units', 'Normalized', 'Position', [0.2 0.2 0.7 0.1], 'String', 'Say hello', 'Callback', @onSayHello);
%]
end

%% --- Event handlers
function [] = onStartScript(sender, args)
%[
    for i = 1:10,
        disp(i);
        pinv(rand(1200, 1200)); % Simulates long processing           
    end
%]
end
function [] = onSayHello(sender, args)
%[
    disp('Hello');
%]
end

It creates a simple figure with two push buttons (one to start the loop and one to simply display Hello text in the command window):

enter image description here

If you run this code by clicking Start script button and then clicking Say hello button, you will see that Hello text will only appear when the loop is completed:

>> mygui
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
 Hello
 Hello
 Hello
 Hello
 Hello

What is happening here is that matlab is locked executing your code while the operating system is still stacking message in it's message queue to indicate that the Say hello button was pressed. It is only when matlab returns to idle state that it can process these messages/events.

To force maltab to process it's message queue add a call to drawnow during the loop:

function [] = onStartScript(sender, args)
%[
    for i = 1:10,
        disp(i);
        pinv(rand(1200, 1200)); % Simulates long processing
        drawnow; % FORCE PROCESSING ANY PENDING GRAPHICAL EVENTS
    end
%]
end

You'll now see that GUI events are processed while the loop is executing:

>> mygui
     1
     2
 Hello
 Hello
     3
     4
     5
 Hello
     6
     7
 Hello
 Hello
     8
     9
    10
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
  • when it is executing some code (i.e. your while loop) it cannot process any other events (i.e. your pushbutton) until code is finished. I this I just overturned this statement and acheived the result with assignin(at the callback function) and evalin(at the external function in external file). But the only problem was that, this pushbutton data should be present always in workspace. – cppiscute Feb 12 '16 at 15:59