File: ef230-2023-08/www/examples/imagemenu.m Download
% Image menu example
% Shows a set of images and allows the user to select one
%  University of Tennessee : EF 230 Spring, 2018
 
function main
clear all; close all; clc;
 
% set up image names and locations
names={'Dr. B','LazyDog','Ethan','BeerGuy','MATLAB Baby'};
urls={'https://ef.engr.utk.edu/efp/pix/abiegals.jpg',
    'https://ef.engr.utk.edu/ef230/img/lazy-dog-wallpaper.jpg',
    'https://ef.engr.utk.edu/efp/pix/edeakins.jpg',
    'https://ef.engr.utk.edu/ef230/img/assess/funny.jpg',
    'https://ef.engr.utk.edu/ef230/img/assess/MATLAB0ME1.jpg'
    };
options = weboptions('CertificateFilename','');  % required for https
players={'abiegals.jpg','lazy-dog-wallpaper.jpg','edeakins.jpg','funny.jpg', 'MATLAB0ME1.jpg'}
for i=1:length(urls)
tmpfile = websave([players{i}],[urls{i}], options);
end


while length(players)>=2
  msg = 'Who do you want to kick off the island?';
  choice = imgmenu(msg,players);
 
  msg=sprintf('Bye Bye %s',names{choice});
  waitfor(msgbox(msg));
 players(choice)=[]; % remove them from the list
  names(choice)=[];
end
img = imread(players{1});
image(img);
title(sprintf('%s prevails!',names{1}));
axis off
end
 
 
function choice = imgmenu(msg,imgs)
% Shows a set of images and allows the user to select one
 
% init
num = length(imgs);
choice = 0;
 
% error check
if num==0
    error('Invalid input');
end
% set up a plot
figure(1);
axis([.5 num+.5 0 1]);
axis equal
axis image
axis off;
set(gca,'ydir','reverse');
hold on;
title(msg);
% load and show the images
for i=1:num
    img=imread(imgs{i}); % load the image
    info = imfinfo(imgs{i}); %
    w = info.Width;
    h = info.Height;
    sc = min(1./w,1./h); % scale factor to kept correct aspect ratio
    x1 = i-w.*sc./2;
    x2 = i+w.*sc./2;
    y1 = 0;
    y2 = h.*sc;
    image([x1 x2],[y1 y2],img);
end
 
% loop until user makes a selection
while true
    [x y] = ginput(1); % get one graphic input point
    rx=round(x); % round off the x value
    if rx>=1 && rx<=num && y>=0 && y<=1
        choice = rx;
        break; % break out of the infinite loop
    else
        waitfor(msgbox('bad click'));
    end
end
 
% got a good choice - show it
close(1);
end