getting data from switch cases?

13 views (last 30 days)
Bex G
Bex G on 7 Dec 2014
Commented: Image Analyst on 8 Dec 2014
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*

Answers (1)

Image Analyst
Image Analyst on 7 Dec 2014
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 Comments
Bex G
Bex G on 8 Dec 2014
sorry sound should of said phoneme, ive changed it now. My code all works but I'm just trying to save the user input all together. I'm not sure why it will only save the last user input rather than all the inputs
Image Analyst
Image Analyst on 8 Dec 2014
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;

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!