フィルターのクリア

random extraction of files from a folder

10 ビュー (過去 30 日間)
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman 2017 年 3 月 16 日
I have a folder of 12000 images (in bmp format). I would like to randomly extract 2000 images and store it in a different folder. How can i do that?

採用された回答

Jan
Jan 2017 年 3 月 16 日
Based on Adam's suggestions:
Dest = 'C:\Temp';
FileList = dir(fullfile(Folder, '*.bmp'));
Index = randperm(numel(FileList), 2000);
for k = 1:2000
Source = fullfile(Folder, FileList(index(k)).name);
copyfile(Source, Dest);
end
Or movefile.
  1 件のコメント
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman 2017 年 3 月 16 日
Many thanks. It perfectly works..

サインインしてコメントする。

その他の回答 (2 件)

Adam
Adam 2017 年 3 月 16 日
doc dir
can be used to give you a listing.
doc randperm
can be applied to your listing to select the random files. I don't have time (or inclination) to write the code for you though so feel free to ask further questions once you have made an attempt if you get stuck.
  1 件のコメント
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman 2017 年 3 月 16 日
i wrote the following and it is giving error. Kindly help rectify the code:
myFolder1 = 'Q:\chromosomal Data\Data\16'; %containing 12000 fles
filePattern1 = fullfile(myFolder1, '*.bmp');
theFiles1 = dir(filePattern1); % getting a 208x1 struct with 6 fields, containing name, folder, date, bytes, isdir,and datenum
whos theFiles1 % No semicolon!
numFiles1 = length(theFiles1) % No semicolon!
if numFiles1 == 0 || isempty(theFiles1)
warningMessage = 'No files found';
uiwait(warndlg(warningMessage));
return;
end
p = randperm(theFiles1(2000)); %error index exceeds matrix dimensions
Kindly help to move the random 2000 files to a different folder. name it anything you like.

サインインしてコメントする。


ES
ES 2017 年 3 月 16 日
You can do a dir in that folder for a list of files. Use a random number generator of 2000 length.
Select the file based on the random number.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by