I have set of image in one folder. How can I display random image from a folder when i click on a pushbutton?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Dez. 2015

0 Stimmen

%first you need to get the names of the images
projectdir = '/homes/users/hani/MATLAB/plan9/images'; %set as appropriate
img_exts = {'bmp', 'gif', 'jpg', 'png', 'tif'}; %list all the extensions you want to include
dinfo = [];
for K = 1 : length(img_exts)
dinfo = vertcat(dinfo, ...
dir( fullfile(projectdir, ['*.', img_exts{K}]) ) );
end
%now pick one randomly
randidx = randi( length(dinfo) );
randomfile = fullfile( projectdir, dinfo(randidx).name );
%read it
imagedata = imread(randomfile);
image(handles.TheDestinationAxes, imagedata); %display it
You will find it more efficient to create dinfo only once and store the data somewhere to be used when the button is pushed.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by