How to execute statements within functions in a random order?
2 views (last 30 days)
Show older comments
'm relatively new to Matlab, and I've been trying to solve my problem for ages but I'm just continuously arriving at a dead end.
I have a code which should, in theory, play 3 sounds in a random order (each order being different for each trial). Upon each sound playing the participant will be asked which sound they heard and then given feedback. I have all the code complete and working up until the random order part. I have created code that on each trial will randomly order 1,2 and 3.
Order = [1, 2, 3];
PhonemeOrder = randperm (numel(Order));
I then have a function which plays the sound/asks the questions etc. within this I have attempted switch cases statements and if else statements depending on the number that PhonemeOrder produces but the order doesnt change even when phoneme order does. I believe my problem is however that PhonemeOrder comes out like [1,2,3] or [3,1,2] which is what i wanted. but Im not sure how to get my sounds to play in the order that it shows because I am using code like...
if/ PhonemeOrder = 1;
then do this...
elseif phonemeorder = 2;
then do this...
else
do this...
Or I've tried code like
switch cases
case 1
do this
case 2
do this
case 3
do this
I'm guessing this is where i am going wrong, but i just dont know how to change it and make it work! I hope this makes sense? I just need it to play in the order that phonemeorder specifies, with the order changing on each trial.
It's probably a really simple solution but the continuous errors and the amount of hours I've tried changing it has finally made me ask! Any help will be greatly appreciated :D
1 Comment
David Young
on 8 Dec 2014
It is possible to do what you want using (for example) an array of function handles - but there may be a simpler way, by reordering the elements of a cell or struct array of sound recordings. Can you give an example of the code that actually generates the sound?
Answers (3)
Guillaume
on 9 Dec 2014
Edited: Guillaume
on 9 Dec 2014
The way I would do this:
sounds = {'ba.wav', 'da.wav', 'ga.wav', 'fa.wav'}; %or whatever you want
numtrials = 2; %or whatever you want
%reorder numtrials * sounds in a random permutation:
orderedidx = repmat(1:numel(sounds), 1, numtrials);
p = randperm(numtrials * numel(sounds));
randsounds = sounds(orderedidx(p));
%play the sounds in the random order:
for sound = randsounds
sound = sound{1};
[fs, y] = audioread(sound);
%... rest of the code
end
0 Comments
Sean de Wolski
on 8 Dec 2014
Edited: Sean de Wolski
on 8 Dec 2014
phoneorder = randperm(3);
for ii = 1:numel(phoneorder)
disp(phoneorder(ii))
switch phoneorder(ii)
% Play AC/DC at various different sample rates
case 1
sound(rand(10000,1),16000)
case 2
sound(rand(10000,1),3000)
case 3
sound(rand(10000,1),50000)
end
end
0 Comments
David Young
on 8 Dec 2014
One possible structure - but see my comment above.
for trial = 1:3
switch phonemeorder(trial)
case 1
< code for first sound >
case 2
< code for second sound >
case 3
< code for third sound >
end
end
4 Comments
David Young
on 15 Dec 2014
Just a belated note to say that the better solution is that given by Guillaume, which simply rearranges the contents of a cell array.
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!