フィルターのクリア

Randomly accessing a file from a folder using Matlab

10 ビュー (過去 30 日間)
KT
KT 2012 年 4 月 13 日
I am trying to randomly access a file from a folder using matlab, could you suggest me that how shall i go about, i am planning to use randn function? but i dnt understand i shall i merge it !

採用された回答

Geoff
Geoff 2012 年 4 月 13 日
I understand your question in two different ways:
1. You want to choose a random file from a specified folder
2. You want to choose a random position in a single file in a folder.
I use randi here, but if you want to use randn you probably need to find a way to turn that into an integer. It's unusual to want to use a normal distribution for something like you seem to be describing though.
So...
If (1) Use the dir command to list files in that folder:
f = dir( myfolder );
ridx = randi(numel(f));
disp( ['Chosen file is: ' f(ridx).name] );
If (2) Open your file and determine its size, then seek to random position:
fid = fopen( myfile, 'rb' );
fseek(fid, 0, 'eof' );
fsize = ftell(fid);
rpos = randi(fsize)-1;
fseek(fid, rpos, 'bof');
...
fclose(fid);
  1 件のコメント
KT
KT 2012 年 4 月 13 日
yes i wanted to do what you mentioned in your option 1 , thank you so much,could you tell me that i have 5 files in my folder, but when i use *numel* , it gives 7 instead of 5, where two files were turning out to be in the form of *..* so do you know the reason for it?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by