Clear Filters
Clear Filters

Inserting images into a code when certain conditions are being met?

3 views (last 30 days)
I'm very very new to coding & currently trying to edit a code for an experiment I'm running at the end of this month. I'd appreciate any guidance (like what I should be looking into... general direction help. I feel very lost).
Basically, I'm trying to edit this code so that I can assign subjects to one of 3 conditions (Upward, Lateral, Downward). After block 2, I need to insert 3 possible jpeg images associated with 3 experimental conditions (U, L, D). I'm attaching the part of the code I assume I need to edit below.
Q1: Without really understanding coding, my assumption's that it'll be something like "if block == 2" (to insert the possible conditions if block 2 has been completed), "elseif block < nblock" (typical block ending message if the block isn't 2, or the last one), "else" (end of experiment).
Am I on the right track?
Q2: I need to have a way to link these 3 images to the 3 conditions, then a way to assign subjects these conditions (U, L, D) before the experiment begins. Where should I look to learn how to do this?
Hope this makes sense, thank you for any guidance you can offer!
if (block > 0)
if UseEL == 1
Eyelink('StopRecording');
end
blockacc = round(blockacc/trial*100);
Screen('TextSize', w,30);
if block < nblock
message = strcat('In this block, your accuracy was\n',num2str(blockacc),'%.\n\nHave a short break.\n\nPress SPACE to start the next block');
else
message = strcat('In this block, your accuracy was\n',num2str(blockacc),'%.\n\nGreat job! You have finished the experiment!');
end
DrawFormattedText(w, message, 'center', 'center', white);
Screen('Flip', w);
WaitSecs(.5);
while (1)
[~,~,keyCode ] = KbCheck;
if keyCode(spaceKey)
break
elseif keyCode(escapeKey)
if UseEL == 1
Eyelink('StopRecording');
Eyelink('CloseFile');
Eyelink('ReceiveFile');
Eyelink('Shutdown');
end
ListenChar(1);
Screen('CloseAll');
ShowCursor;
fclose('all');
Priority(0);
clear all
break;
end
end
FlushEvents('keyDown');
end

Answers (1)

Samay Sagar
Samay Sagar on 23 Feb 2024
To add the functionality for displaying specific images after block 2 and to assign subjects to one of the three conditions, you can follow these steps:
You can insert a check for block == 2 to display the images associated with the experimental conditions. Here's how you can modify the code:
if (block > 0)
% ... [existing code] ...
if block == 2
% Get the image associated with the subject's condition
conditionImage = GetConditionImage(subjectCondition);
image(conditionImage);% Display the image)
elseif block < nblock
% ... [existing code] ...
else
% ... [existing code] ...
end
% ... [existing code ]
end
To assign subjects to conditions and link images to those conditions, you can follow these steps:
  1. Define Conditions and Images: At the beginning of your script, define a mapping between conditions and image file paths. You can use “struct” for this.
conditionImages = struct('U', 'upward.jpg', 'L', 'lateral.jpg', 'D', 'downward.jpg')
2. Create the GetConditionImage Function: This function will return the image associated with the condition
function image = GetConditionImage(condition)
conditionImages = struct('U', 'upward.jpg', 'L', 'lateral.jpg', 'D', 'downward.jpg');
image = imread(conditionImages.(condition));
end
3. You can now assign subject to conditions based on your use case. Here’s how you can assign it randomly
onditions = {'U', 'L', 'D'};
subjectCondition = conditions{randi(numel(conditions))};
Read more about “imread” and “struct” here:

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!