MATLAB: Error during selecting dynamically image from folder

randirandiperm

Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or
equal to IMAX.
Error in tt (line 25)
thisfile = files(randi(num_files)).name;
clc;
clear all;
tic;
%% Training images
id_test = 0;
id_train = 0;
for folder_idx = 1 : 10 % no of classes 100
for i = 1 : 8 % no of images per class 8
%thisfile = fullfile('ROITrain', num2str(folder_idx), [num2str(i) '.bmp ']);
files = dir( fullfile('ROITrain', num2str(folder_idx), [num2str(i) '.bmp ']));
num_files = numel(files);
thisfile = files(randi(num_files)).name;
%image = imread(filename);
B = imread(thisfile );
X = double(B);
X = imresize(X,[300 250],'bilinear'); %300 250
id_train = id_train+1;
traindata{id_train}=ext_vein(X,1);
traindata = traindata';
% only four minutie is taken from one image
reduced_traindata = cellfun(@(M) M(1:min(end,4), :), traindata, 'uniform', 0);
end
end
save('db2.mat','reduced_traindata');
toc

Best Answer

"I want to randomaly select 8 images from specific folder how i can do that"
Something like this:
P = 'path to the folder where the files are saved';
S = dir(fullfile(P,'*.bmp'));
N = numel(S);
X = randperm(N);
F = {S(X(1:8)).name} % cell array of 8 randomly selected filenames from that folder