MATLAB Answers

How to change images dynamically with radio buttons in app designer?

조회 수: 16(최근 30일)
Hamid Kilic
Hamid Kilic 2021년 7월 27일
편집: Cris LaPierre 2021년 7월 28일
I have a question regarding radio buttons in Matlab app designer. I am also adding photo of the UI for clear understanding. First, I want to load two images from my local directory and show them on UIaxes when the corresponding radio button is chosen. However, let's say I load the first image by clicking Image 1 button and even Image 1 radio button is chosen the image isn't loaded on UIaxes immediately. However, If I change the radio button to the second one and then click the first one Image 1 is loaded on UIaxes. So in short, How can I make loading of image in UIaxes dynamically?
Here is so far what I attempted, I only added the lines of code that I wrote:
properties (Access = private)
FirstImage
SecondImage
end
methods (Access = private)
% Button pushed function: Image1Button
function Image1ButtonPushed(app, event)
[filename,filepath] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'});
fullname = [filepath, filename];
if isnumeric(filename); return; end
app.FirstImage = imread(fullname);
end
% Button pushed function: Image2Button
function Image2ButtonPushed(app, event)
[filename,filepath] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'});
fullname = [filepath, filename];
if isnumeric(filename); return; end
app.SecondImage = imread(fullname);
end
function ChangeimageButtonGroupSelectionChanged(app, event)
if app.Image1.Value
imshow(app.FirstImage, 'Parent', app.UIAxes);
elseif app.Image2.Value
imshow(app.SecondImage, 'Parent', app.UIAxes);
end
end
end
  댓글 수: 3
Hamid Kilic
Hamid Kilic 2021년 7월 27일
I don't get an error my code works but when I choose an image from my local diractory, the image isn't shown immediatly. For the image to be shown, I have to click the second radio button and again I have to click the first button and then I see the image. My problem is I cannot see the image when It is loadded the first time. I know that I have to add something to fire the action but I don't knoe how to do it, so I think something is missing in my code. I hope I could explain.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 7월 27일
The radio buttion group callback is executed when the selection changes. That is why you have to select image 2 then image 1 to get image 1 to appear (i.e. you have to change the selection).
If you want image 1 to be loaded when the app opens, consider adding a startup callback function to load image 1 to the axes initially.
  댓글 수: 4
Cris LaPierre
Cris LaPierre 2021년 7월 28일
That's just redundant code. You should do either my approach or yours, but not both.
My approach calls the ChangeimageButtonGroupSelectionChanged function. If you look in your function declaration line, this function expects two inputs: app and event. In that context, app.ChangeimageButtonGroup is your event input, meaning the invoking component. It is unused in your function, so really you could put anything for the 2nd input. If just needs to have two.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

범주

Community Treasure Hunt

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

Start Hunting!

Translated by