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

12 次查看(过去 30 天)
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.

采纳的回答

Geoff Hayes
Geoff Hayes 2021-12-17
@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 个评论
Kyle Ramphal
Kyle Ramphal 2021-12-18
编辑:Kyle Ramphal 2021-12-18
Thank you for your response. Just one clarification with respect to the code. When you ment 'n' I would need to manually input the image codeline inorder to get the multiple images for example with imahed 1 " mImg1 = imread('filename'); ". Another question is that can i integrate the variable mPosIffset with a counter as everytime the button is pressed one images shows up instead of having all show up at once? or would I have to place the set(mHI,....) within the call back functon of the push button itself? Since i would like the user to have the option to input any amount of images they wish.
Geoff Hayes
Geoff Hayes 2021-12-18
@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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by