0

i'm am very new to Matlab but really want improve. For my experiment i want to show a picture which the participant response yes/no to, using two different keys (f&g) and then the next picture is presented and it repeats so onward.

Presenting the picture, using the keys works for far, but i can't get it to repeat the trial. Thus my question is how can i get the program to repeat/loop my trial? Is there something wrong in my code so far or is there additional coding i should use?

this is my code so far

function try1_6()

cleanupObj= onCleanup(@() myCleanupFxn);

% PRETEST
% Initialize screen with black background
winID = Screen('openWindow',0, [0 0 0]);

%Parameter
backcol=255;
textcol=0;

% Load image file(s)
structimages= [];
TheImagesdir = dir('theImagesdir/*.jpg');
for i=1: length(TheImagesdir);
    TheImages  = imread(['theImagesdir/' TheImagesdir(i).name], 'JPEG');

    % Get width and height
    imageX = size(TheImages,2);
    imageY = size(TheImages,1);

    % Convert to texture
    myTexture = Screen('MakeTexture', winID, TheImages);

    % Set destination rectangle
    destRect = [50  100  50+imageX  100+imageY];

    %save to structure
    structimages(end+1).filename=TheImagesdir(i).name;
    structimages(end).destRect= destRect;
    structimages(end).texture= myTexture;
end

%Make triallist
numberOfItems= [5]; %list of all possible items
Nrepeats=4;
Response=0;
TrialList=HH_mkTrialList({numberOfItems Response},Nrepeats); 


%PRESENTATION

for trialnum=1:size(TrialList,1)
    nitems = TrialList(trialnum,1);

    Screen('FillRect', winID,backcol); % makes the screen blank

    %displays text
    DrawFormattedText(winID,'dkjfghaslkdfglksdjgfh','center','center',textcol);
    Screen('Flip', winID)
    HH_waitForKeyPress({'space'}); % waits for spacebar to be pressed
    Screen('FillRect',winID,backcol);
    Screen('Flip',winID);
    WaitSecs(1);

    %display picture
    whichTheImages= randi(length(TheImagesdir)); % randomly selects image for directory
    Screen('FillRect',winID,backcol);
    Screen('DrawTexture', winID, myTexture, [], destRect);

    Screen('Flip', winID);
    HH_waitForKeyPress({'f','j'},5)

    if resp==-1
       break
    end 

    TrialList(trialnum,4)= response; %records response

end

end

function myCleanupFxn()
    Screen('CloseAll')
end
Setsu
  • 1,188
  • 13
  • 26
  • 1
    One very important thing you forgot to mention is that you are using [Psychtoolbox](http://psychtoolbox.org/) for the screen stuff. – Setsu Mar 30 '15 at 22:00
  • Also, indent your code properly. The body of `try` and `for` blocks should be indented by a tab. Additionally, don't wrap the whole thing in a giant `try`. – Setsu Mar 30 '15 at 22:03
  • This is not a complete question. `HH_mkTrialList` and `HH_waitForKeyPress` are both custom methods that you haven't explained what they do. See [this](http://stackoverflow.com/help/how-to-ask) guideline for posting questions on this site. – Setsu Mar 30 '15 at 22:18
  • What is the specific error? Describe the problem you are having; what happens exactly and why is it wrong? – Setsu Mar 31 '15 at 23:58
  • @Setsu It don't not give a specific error, the code does run. The last picture of the directory does open and it responds to the specific key press I specified. However after pressing so, it all closes, without moving on to the next picture, whereas it should. I think its going wrong because I don't fully get what each line of code does (much of it I get, but not the part that is suppose to code for looping, the "for trialnum=1:size(triallist1) nitems = TrialList(trialnum,1);" bit). – M Druyvesteyn Apr 01 '15 at 00:58
  • Variables `resp` and `response` are still used before they are defined. Also, you can't use `myTexture` directly because it will never update itself to other images stored in the structure. Finally, if your code is exiting without errors, then `TrialList` does not contain what you expected it to. – Setsu Apr 01 '15 at 18:26

1 Answers1

0

There are a number of problems with you code that you need to address. First of all, TrialList is used before it is declared/initialized. The Make triallist block of code seems out of place in the body of the for loop, and should probably be placed before you loop TrialList.

Your second problem is the inner for loop that loads images. Right now, it loads every image found in the directory, on every trial! There is no reason for you to be doing this, and you should be placing this for loop outside the trial loop as well. Furthermore, your original code never worked as intended, because you never save the loaded texture anywhere; myTexture is overwritten by the last image in your folder and that's the only texture you're ever gonna get. So in addition to pre-loading the images before the loop, you need to save them in a data structure so that you can use them later in your trial loop. A simple struct will work nicely here:

structImages = [];
TheImagesdir = dir('theImagesdir/*.jpg');
for i = 1:length(TheImagesdir);
    TheImages  = imread(['theImagesdir/' TheImagesdir(i).name], 'JPEG');

    % Get width and height
    imageX = size(TheImages,2);
    imageY = size(TheImages,1);

    % Convert to texture
    myTexture = Screen('MakeTexture', winID, TheImages);

    % Set destination rectangle
    destRect = [50  100  50+imageX  100+imageY];

    %save to structure
    structImages(end+1).filename = TheImagesdir(i).name;
    structImages(end).destRect = destRect;
    structImages(end).texture = myTexture;
end

There are other inconsistencies in your code:

  1. whichTheIamges is defined but not used
  2. resp is used in the comparison if resp==-1 but is not defined
  3. response is saved into TrialList before it is defined

Finally, the biggest problem is Screen('CloseAll', winID); is inside the trial loop, so you tear down your whole presentation platform after the first trial.

FYI, as noted in my comment wrapping your entire script in a try block is really poor practice. I suspect you do this because you want to be able to Ctrl+C mid-task, but there's a better way to do this. If you make your entire script a function then you can use the onCleanup method to execute code whenever your function exits (whether normally, by error, or by interruption). The method goes like this:

function myScript()
%//make your script a function. There is an additional advantages to doing this: 
%//function performance is better than script performance.

%//blah-blah-blah

%//setup the cleanup object before opening screen
cleanupObj = onCleanup(@() myCleanupFxn);

%//open the screen
winID = Screen('openWindow',0, [0 0 0]);

%//blah-blah-blah

end

function myCleanupFxn()
    %//local function, not visible outside of this file
    Screen('CloseAll');
end
Setsu
  • 1,188
  • 13
  • 26
  • thank you so much for answering, really. i get most of the comments you made, and clarified for problems for me. i tried to apply your comments the best i could, but unsuccessfully it seems. i cant seem to include the code in the comment ( i also new to this site) so ill change it in the original question i quess. – M Druyvesteyn Mar 31 '15 at 22:06
  • @MDruyvesteyn Please look at the how-to guideline on asking questions. Each question should have something concrete to answer, and not just "my code doesn't work". – Setsu Mar 31 '15 at 22:39
  • oke, ill try again: how can i get the program to repeat/loop my trial? ultimately i want to have a picture+key press, repeat this for a number of times and record the key press response – M Druyvesteyn Mar 31 '15 at 23:49