I am trying to randomly access a file from a folder using matlab. Could you suggest how to go about it? I am planning to use randn
function, but i don't understand how.
Asked
Active
Viewed 406 times
0
-
Is your problem randomly picking a file from a folder? – trutheality Apr 13 '12 at 04:46
-
yes, this is the exact i want to do – KT Harris Apr 13 '12 at 05:25
1 Answers
2
I think you need a random number with uniform distribution, not normal. So here you go ..
Files = dir('MyFolder');
N = size(Files,1) - 2;
rand = randi(N,1);
Random_filename = Files(rand+2,1).name;

nac
- 411
- 4
- 6
-
1`randi` is what you want. But you should also remove *all* directories, not just ./ and ../ (which are the reason for the -2/+2 here). In the code here, you could do that with `Files = Files(~getfield(Files,'isdir'));` – tmpearce Apr 13 '12 at 06:15
-
That's true. I just kept it simple for understanding and figured he could solve that part by himself. Now this completes the answer. – nac Apr 13 '12 at 06:33