MATLAB: How to update an image displayed in a GUI without it wiping the buttons/etc

refresh imageupdating figureupdating gui

I am trying to make a GUI that has two buttons to allow the user to do two things: rotate an image in 90 degree steps, and exit/save progress. I'm stuck on the first step. I've set up the GUI as follows:
gui_figure = figure('Visible', 'off');
imshow(original_image);
% create buttons
rotate_but = uicontrol('Style', 'pushbutton', 'String', ...
'Rotate', 'Position', [50 20 50 20], 'Callback', @rotate);
exit_loop = uicontrol('Style', 'pushbutton', 'String', ...
'Done', 'Position', [120 20 50 20], 'Callback', 'close(gui_figure)');
set(gui_figure, 'Visible', 'on');
and the callback to rotate as:
function rotate(hObject, event, handles)
original_image = imrotate(original_image, 270);
imshow(original_image);
end
But this merely shows the rotated image without the buttons I want. I've tried to instead update the actual figure's data:
set(gui_figure, 'UserData', original_image);
refreshdata(gui_figure);
But I think I'm missing something on how 'refreshdata' (and 'refresh' for that matter) work. I've also tried using the 'guidata' command to store gui data but I don't know if I even came close to using that properly – does it only restore data to elements that are already present? If so that approach was fruitless.

Best Answer

gui_figure = figure('Visible', 'off');
imhandle = imshow(original_image);
% create buttons
rotate_but = uicontrol('Style', 'pushbutton', 'String', ...
'Rotate', 'Position', [50 20 50 20], 'Callback', {@rotate, imhandle});
exit_loop = uicontrol('Style', 'pushbutton', 'String', ...
'Done', 'Position', [120 20 50 20], 'Callback', @(src,obj) close(gui_figure));
set(gui_figure, 'Visible', 'on');
function rotate(hObject, event, imhandle)
current_image = get(imhandle, 'CData');
current_image = imrotate(current_image, 270);
set(imhandle, 'CData', current_image);
end