How to execute statements within functions in a random order?

2 次查看(过去 30 天)
'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 个评论
David Young
David Young 2014-12-8
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?

请先登录,再进行评论。

回答(3 个)

Guillaume
Guillaume 2014-12-9
编辑:Guillaume 2014-12-9
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

Sean de Wolski
Sean de Wolski 2014-12-8
编辑:Sean de Wolski 2014-12-8
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

David Young
David Young 2014-12-8
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 个评论
Bex G
Bex G 2014-12-9
awesome thankyou both so much :D Now I just need to work out how to save all the response on each trial...
Thanks again guys, big help!
David Young
David Young 2014-12-15
Just a belated note to say that the better solution is that given by Guillaume, which simply rearranges the contents of a cell array.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by