getting data from switch cases?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi,
I'm relatively new to MATLAB and for some reason I just cant get the data from switch case loops to save?? I have a function called [data] = playsound. within this function it chooses a random number (either 1,2 or 3) and based on which number it has chosen it must play a particular sound, ask which one was heard and then give feedback. to do this I have used 2 switch cases; one based on the number chosen and one based on the user response (what they think they heard). This function is repeated several times depending on how many trials. I've attempted to save the data but it only seems to save the last sound and response given when I really need the sound (sound1,sound2,sound1)etc alongside their responses to these sounds (R2, R2,R2). in other words when it requires 5 trials of this function instead of 5 sounds and 5 responses I'm only getting 1.
I've provided a basic example of my code, with case 2 and 3 being a repeat of the first case
function [data] = playsound
Phoneme= {'1','2','3'};
idx = randperm (numel(Phoneme));
P = Phoneme{idx(1:1)};
data.Phoneme = Phoneme;
switch P
case '1'
pause(1)
soundsc(y, freq);
response = questdlg('What did you hear?',...
'?', 'bo','up','no','no');
switch response
case 'bo'
(msgbox ('the answer was bo'))
response = 'bo';
case 'up'
(msgbox ('the answer was bo'))
response = 'up';
case 'no'
(msgbox ('the answer was bo'))
garesponse = 'no';
end
data.response = response;
case 2 .... case 3....
any help would be greatly appreciated, I just cant get it?
*I have a main function which runs the code and links all my other functions and the data together to hopefully display it the end of the code run*
답변 (1개)
Image Analyst
2014년 12월 7일
It doesn't look like Phoneme is defined anywhere before it's used for the first time. What happens if you just step through the code in the debugger, like any of us would do if we were to debug your code? See this if you don't know how to do that: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
댓글 수: 2
Image Analyst
2014년 12월 8일
You probably have to make data a global variable and index it
global data;
% Find out how long it is right now,
% before we append another structure to the array.
numStructs = length(data);
thisIndex = numStructs + 1; % Add on to the end of the structure array.
data(thisIndex).Phoneme = Phoneme;
data(thisIndex).response = response;
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!