Question

I have a MATLAB Guide project. I have a mixture of regular functions and callbacks in the same .m file.

I call a function from within a callback and as that functions runs through a for loop I wish for a string box to be updated. Here is an example:

From a callback (extract shown) I call this function:

[color] = get_color(images, handles);
set(handles.ProcessImage, 'string', 'Processing Complete');

The get_color function is located within the same .m file

function [color_corrections] = get_color(images, handles) 

[n, ~, ~, ~] = size(images); % Find the number of images

for imgIdx=1:n % For each image

    set(handles.ProcessImage, 'String', 'Processing Image #');

end
end

The problem is that handles.ProcessImage does not get updated during the for loop, yet it gets written to 'Processing Complete' when it returns from the function.

Any thoughts?

Thanks, TommyMac

Was it helpful?

Solution

In a tight loop, the calculations take precedence so Matlab ends up not bothering to update the GUI until it has time again, i.e. once the loop's finished. You can force a GUI update by calling drawnow after updating the string, like so:

set(handles.ProcessImage, 'String', 'Processing Image #');
drawnow; % force the new string to be rendered

Be aware that you could take a massive performance hit, if the code in the loop is short but has many iterations. If there are relatively few iterations but each one takes a long time, you shouldn't notice the difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top