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

조회 수: 10 (최근 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