How to display an image every time a button is pushed in App designer?

11 views (last 30 days)
Kyle Ramphal
Kyle Ramphal on 17 Dec 2021
Commented: Geoff Hayes on 18 Dec 2021
The question is based on creating a push button that displays an image in every instance the button is pressed. For example if the user pushes the button 5 times I would like 5 images to be displayed on the UI.Axes at different positions. I would like to know of a way how i can achieve this fucntion. I am fairly new to matlab app desinger. Any assistance is appreciated. The attached zip file is the code I had so far.

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Dec 2021
@Kyle Ramphal - you might instead try inserting the n copies of an image within a larger image. Your larger image would act as the background, and you would then place your other images within it. For example,
mImg1 = imread('gfci.PNG');
mImg2 = imread('120V.png');
% create the background image which will be black
mBackgroundImg = uint8(zeros(1000,1000,3));
% create the figues, axes, and image
mHFig = figure;
mHAxes = axes;
mHImg = image(mHAxes, mBackgroundImg);
mImg1Size = size(mImg1);
mImg2Size = size(mImg2);
% insert the first image to the top left corner of the background image
mBackgroundImg(1:mImg1Size(1),1:mImg1Size(2),:) = mImg1;
% update the image object data
set(mHImg, 'CData', mBackgroundImg);
% position offset from top left corner of where the second image should be placed
mPosOffset = 500;
mBackgroundImg(mPosOffset:mImg2Size(1) + mPosOffset - 1,mPosOffset:mImg2Size(2) + mPosOffset - 1,:) = mImg2;
% update the image object data
set(mHImg, 'CData', mBackgroundImg);
You should be able to do something similar with your app so long as your background image is large enough and you know where you want to insert the new image.
  2 Comments
Geoff Hayes
Geoff Hayes on 18 Dec 2021
@Kyle Ramphal - my mention of n is just to indicate any number of images that you add. You can use the above code within your app to allow the user to add any number of images, but you will need to decide the positions for them.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!