How to load the images using the code approach

Hello,
I have a set of images in the folder of the app project. I am trying to implement the following.
Based on the dropdown selection, a desired image should be loaded to the app window using UIAxies or Image, as shown in the screenshot.
1-How to load the images using the code approach?
2-Which is better to load the image UIAxies or Image?

 채택된 답변

Kevin Holly
Kevin Holly 2023년 1월 30일
편집: Kevin Holly 2023년 1월 30일

1 개 추천

1.
properties
folder
filename
end
% Call back for pushbutton to load image
[app.filename, app.folder] = uigetfile('*.*');
if ~ischar(app.filename); return; end %user cancel
[~,~,ext] = fileparts(app.filename);
files = dir(fullfile(app.folder,['*' ext]));
% Update dropdown box with file names in the same folder with the same extension as file loaded.
app.ImageFileDropDown.Items = {files.name};
app.ImageFileDropDown.Value = app.filename;
% Read image
Img = imread(fullfile(app.folder,app.filename)); % or imread(fullfile(app.folder,app.ImageFileDropDown.Value))
imshow(app.UIAxes) % to load on axes
app.Image.ImageSource = fullfile(app.folder,app.filename) % to load on Image
2. If you plan on ploting or playing graphical objects ontop of image, I would use app.UIAxes.

댓글 수: 4

Thank you.
I do not want the user to select an image from the image folder. I want the user to choose from the dropdown, and I load the appropriate image in the code. For example, in the dropdown ( cat, car, dog) let's say the user selects cat. The picture of the cat will show up in the app. Image.
Kevin Holly
Kevin Holly 2023년 1월 30일
편집: Kevin Holly 2023년 1월 30일
In that case, I would use the Image instead of UIAxes and you can create a callback to the drop down box that does the following for each case:
Switch app.ImageFileDropDown.Value
case 'cat'
app.Image.ImageSource = 'catfile.png'
case 'dog'
app.Image.ImageSource = 'dogfile.png'
end
Thank you
You can configure ItemsData to return particular values for a choice, so you can configure Items to be the list of things the user is shown, and ItemsData to be the corresponding filenames to use.
Img = imread(fullfile(app.folder,app.filename)); % or imread(fullfile(app.folder,app.ImageFileDropDown.Value))
imshow(app.UIAxes) % to load on axes
That should be
Img = imread(fullfile(app.folder,app.filename)); % or imread(fullfile(app.folder,app.ImageFileDropDown.Value))
imshow(app.UIAxes, Img) % to load on axes

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 1월 30일

댓글:

2023년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by