How to change the Background for GUI

4 views (last 30 days)
i made the image as background for GUI,???? but i want to add button when i will press it will change the background to another image ??? how
ah = axes('unit', 'normalized', 'position', [0 0 1 1]);
% import the background image and show it on the axes
bg = imread('work/bg_red.jpg'); imagesc(bg);
% prevent plotting over the background and turn the axis off
set(ah,'handlevisibility','off','visible','off')
% making sure the background is behind all the other uicontrols
uistack(ah, 'bottom');

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Jul 2016
Mohammed - it looks like you have used some of the code from https://www.mathworks.com/matlabcentral/answers/96023-how-do-i-add-a-background-image-to-my-gui-or-figure-window, so we can extend this to do the following
function GUIBackgroundExample
hFig = figure;
hAxes = axes('Units','normalized','Position',[0 0 1 1]);
uistack(hAxes,'Bottom');
img = imread('myImage1.jpg');
hImg = image(img, 'Parent', hAxes);
set(hAxes,'HandleVisibility','off', 'Visible','off')
hBtn = uicontrol(hFig,'Style','pushbutton','String','Change BG',...
'Units', 'normalized', 'Position',[0.0 0.0 0.2 0.05], ...
'Callback', @ChangeBackgroundCallback);
function ChangeBackgroundCallback(hObject, eventdata)
if ~isempty(hImg)
delete(hImg);
end
img = imread('myImage2.jpg');
hImg = image(img, 'Parent', hAxes);
end
end
Like your code, we create the figure with the axes and load an image. We then create a pushbutton in the lower left corner with a callback function that will delete the existing image (graphics object), read the new one, and then display it in the current axes.
Try the above and see what happens!

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!