0

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.

Backlin
  • 14,612
  • 2
  • 49
  • 81
KT Harris
  • 41
  • 1
  • 10

1 Answers1

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