0

I have function called Assignment in Matlab with PsychToolBox. This function shows a random color to the paritcipant and require participant to name the color and record this data.

function should return me 2 output as a string

  1. rgb code of the random color like: trial(1).color = [5 5 5]
  2. a matrix which correspond to the sound record.

I write the main functions and color part is okay, but I cannot integrate the recording function into the main function.

in main function I use this string trial.data = recording(1,0,5) and then I wrote a subfunction named "recording"

 function recording (wavfilename, voicetrigger, maxsecs)

    bla, bla 

    end

However, the main function does not recognize the subfunction. Am I doing an logical error? the error message is below

Error: File: assignment.m Line: 40 Column: 27 Unexpected MATLAB expression.

line 40 = trial.data = recording(1,0,5)

function ass8(trial) 

Screen('Preference', 'SkipSyncTests', 1)

ListenChar(2);


Screen('HideCursorHelper', 0, 0)

[myWin, rect]=Screen('OpenWindow',0,[128,128,128]);


    centerX=rect(3)/2;
    centerY=rect(4)/2;


for trial = 1:100

    Screen('TextSize', myWin, 30);
    Screen('TextFont', myWin, 'Times');
    [normBoundsRect, offsetBoundsRect] = Screen('TextBounds',myWin, 'What is the color of the rectangle?');
    Screen('DrawText', myWin, 'What is the color of the rectangle?', (centerX-(normBoundsRect(3)/2)),(centerY-(normBoundsRect(4)/2+150)), [0,0,0]);

    Screen('Flip', myWin)
    WaitSecs(1)% inter stimulus interval



    color = randi(255,1,3)
   while 1
    Screen('FillRect', myWin, color ,[583, 284, 783, 484])
%    [ (centerX-100), (centerY-100), (centerX+100),(centerY+100)]); 

    Screen('Flip', myWin)
    WaitSecs(3)

    trial.color = color % trial 'ın rengini belirtmesini söyledim


    trial.data = reco(1,0 5)% trial'ın ismi 1,  kayıt yapacağı süre ise 3 sn


    if Waitsecs(3)==1
        break; % Terminates the loop if the condition is                %  satisfied
    end

   end


    pause(.05);

%  [clicks, x, y, buttons] = GetClicks(myWin);
% 
% buttons=0;
% while ~buttons
%     [x, y, buttons] = GetMouse(myWin);
% end 
%     while 1
%         [x,y,buttons] = GetMouse(myWin);
%         if ~buttons(1)
%             break;
%         end
%     end





 Screen('CloseAll')
end
end





function  reco(wavfilename, voicetrigger, maxsecs)
% 
% AssertOpenGL;

if nargin < 1
    wavfilename = [];
end

if nargin < 2
    voicetrigger = [];
end

if isempty(voicetrigger)
    voicetrigger = 0;
end

if nargin < 3
    maxsecs = [];
end

if isempty(maxsecs)
    maxsecs = inf;
end


InitializePsychSound;


freq = 44100;
pahandle = PsychPortAudio('Open', [], 2, 0, freq, 2);


PsychPortAudio('GetAudioData', pahandle, 10);

PsychPortAudio('Start', pahandle, 0, 0, 1);

if voicetrigger > 0
    % Yes. Fetch audio data and check against threshold:
    level = 0;

    % Repeat as long as below trigger-threshold:
    while level < voicetrigger
        % Fetch current audiodata:
        [audiodata offset overflow tCaptureStart] = PsychPortAudio('GetAudioData', pahandle);

        % Compute maximum signal amplitude in this chunk of data:
        if ~isempty(audiodata)
            level = max(abs(audiodata(1,:)));
        else
            level = 0;
        end

        % Below trigger-threshold?
        if level < voicetrigger
            % Wait for a millisecond before next scan:
            WaitSecs(0.0001);
        end
    end


    else
    % Start with empty sound vector:
    recordedaudio = [];
end


s = PsychPortAudio('GetStatus', pahandle)


while ~KbCheck && ((length(recordedaudio) / s.SampleRate) < maxsecs)
    % Wait a second...
    WaitSecs(1);

    % Query current capture status and print it to the Matlab window:
    s = PsychPortAudio('GetStatus', pahandle);

    % Print it:
    fprintf('\n\nAudio capture started, press any key for about 1 second to quit.\n');
    fprintf('This is some status output of PsychPortAudio:\n');
    disp(s);

    % Retrieve pending audio data from the drivers internal ringbuffer:
    audiodata = PsychPortAudio('GetAudioData', pahandle);
    nrsamples = size(audiodata, 2);

    % Plot it, just for the fun of it:
    plot(1:nrsamples, audiodata(1,:), 'r', 1:nrsamples, audiodata(2,:), 'b');
    drawnow;

    % And attach it to our full sound vector:
    recordedaudio = [recordedaudio audiodata]; %#ok<AGROW>
end





PsychPortAudio('Stop', pahandle);

audiodata = PsychPortAudio('GetAudioData', pahandle);


recordedaudio = [recordedaudio audiodata];


PsychPortAudio('Close', pahandle);

if ~isempty(wavfilename)
    psychwavwrite(transpose(recordedaudio), 44100, 16, wavfilename)
end


fprintf('helal lan!\n');

ListenChar(2);


end
  • The first thing I see is that `recording` doesn't return anything... – beaker Dec 09 '16 at 22:30
  • actually recording function can work independently if I run it as main function, and it returns a wav file in matlab folder but when I look at the sub function examples like you said they have equations, I am so confused:/ –  Dec 10 '16 at 00:18
  • How do you call it (both from the command window and from your function)? How do you get the output when you call it from the command window? Ordinarily you should have something like `function [color data] = recording(...)` – beaker Dec 10 '16 at 00:23
  • if I use the recording function as main function I write command window "recording('trial.wav',0,5) but probably there is also problem with giving dynamic names to the recording files. –  Dec 10 '16 at 00:36
  • I add whole code sample above in the question –  Dec 10 '16 at 00:39
  • You missed a comma before the `5` in `reco(1,0 5)`. – beaker Dec 10 '16 at 00:42
  • omg! thank you, I tried immediately but this time gives this error message –  Dec 10 '16 at 00:46
  • Error using ass8>reco Too many output arguments. Error in ass8 (line 40) trial.data = reco('1.wav',0, 5) –  Dec 10 '16 at 00:47
  • Now we're back to my original statement. You need to have return values from your function `reco`. – beaker Dec 10 '16 at 00:48
  • I tried to give name like 'function [recordedaudio audiodata] = reco(wavfilename, voicetrigger, maxsecs)' but it still gives same error message for the line 'trial.data = reco(1,0, 5)', however, once it returns 2 matrix but ı do not understand why because actually I did not change anything between trials [link](http://prntscr.com/dhjp7y) –  Dec 10 '16 at 01:21

0 Answers0