How to change images dynamically with radio buttons in app designer?
25 views (last 30 days)
Show older comments
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 Comments
TADA
on 27 Jul 2021
I had to change the name of the radio buttons though from what you set, the default name app designer gave them was Image1Button_2 and Image2Button_2 (because Image1Button and Image2Button where taken already by the push buttons)
Accepted Answer
Cris LaPierre
on 27 Jul 2021
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 Comments
Cris LaPierre
on 28 Jul 2021
Edited: Cris LaPierre
on 28 Jul 2021
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.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!