How to execute statements within functions in a random order?

조회 수: 3 (최근 30일)
Bex G
Bex G 2014년 12월 8일
댓글: David Young 2014년 12월 15일
'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