Blue Screen Hang

Running the following code on an eMac OS 10.3.9 hangs the machine at the blue screen. I
have the latest stable version of PTB. This code works fine on a mini mac with the same
OS.

----------------

function skeleton_experiment

% function skeleton_experiment
%
% A skeleton experiment.
%
% Experiment description & motivation:
%
% On each trial:
% Cue
% 750ms delay
% Show image for 500ms
% Display text
% Get an 'F' or 'J' response
% Dbeepisplay which response was hit
% Collect response & RT & store in file
%
% Each experiment block has 1 of each image.
%
% Arguments:
% none
%
% Returns:
% none
%
% Suggested reading:
% http://psychtoolbox.org/questions.html#reaction
% http://vision.nyu.edu/Tips/Keypress.html
% http://www.kyb.tuebingen.mpg.de/bu/people/kleinerm/ptbosx/ptbdocu
-1.0.5MK4R1.html
% http://psychtoolbox.org/library.html
% http://en.wikibooks.org/wiki/Matlab:Psychtoolbox
% The comments and code in the following files:
% DriftDemoOSX2.m
% MovieDemoOSX.m
% OldNewRecogExp.m
%
% Always get latest verstion of PsychToolbox (psychtoolbox.org)
%
% Andrew Cohen
% 10/14/05
%
% Known Bugs:
% NOTE: TEXT CENTERING NOT IMPLEMENTED
%
% Change History:
% Document dated changes here...

try

% This script calls Psychtoolbox commands available only in OpenGL-based
% versions of the Psychtoolbox.
AssertOpenGL;

% Constants ------------------------------------------------------

% Experiment constants
num_blocks = 1 % Number of blocks

% Timing
cue_time = .750; % Time between cue onset and stimulus
stim_time = 1.500; % How long is stimulus displayed
feedback_time = .2; % How long to display feedback


% Output files
exp_name = 'mds_sim_exp';
data_folder = 'Data/';
data_file_postfix = '.dat';
out_file_prefix = [data_folder exp_name '_data_']; % What to call the output file
out_header_prefix = [data_folder exp_name '_header_']; % What to call the header file (to
store info about data format, etc)

% Input files
text_folder = 'Text/';
instructions_file = 'Instructions.txt';
warm_up_file = 'Warm_up_instructions.txt';
more_instructions_file = 'More_Instructions.txt';
debriefing_file = 'Debriefing.txt';
text_line_height = 30
text_line_top_margin = 20
text_line_left_margin = 20

% Font constants
font = 'Times';
font_size = 24;
font_style = 0;

% The screens
% If there are multiple displays guess that one without the menu bar is the
% best choice. Dislay 0 has the menu bar.
screens = Screen('Screens');
screen_number = max(screens);

% Color constants
% Find the color values which correspond to white and black.
white = WhiteIndex(screen_number);
black = BlackIndex(screen_number);
gray = (white+black)/2;
if round(gray) == white
gray = black;
end

% Feedback beep
vol = .2; %Volume
freq_f = .1; % Fequency
freq_j = .2; % Fequency
dur = 500; % Duration (ms)
rate = Snd('DefaultRate'); % Sampling rate

% Visual cue
cue_length = 30; % Half of the line
cue_width = 4; % Wideth of the line

% Priority (how tight of control should psychtoolbox have of the computer)
max_priority_level = MaxPriority(screen_number);
min_priority_level = 0;

% For determining where to get the input (use 'a = PsychHID('Devices')'
input_device = 3; % Usually = 1

% Escape keys (to quit experiment)
esc_keys = {'ESCAPE' 'LeftShift' 'RightShift'};

% TEMPORARY: CHANGE LATER
text_x_loc = 550;


% Psychtoolbox setup
----------------------------------------------------------

% Change directory to the folder that contains this function
main_file = [mfilename, '.m'];
eval(['main_path = which(''' main_file ''');']);
main_path = main_path(1:end-length(main_file));
cd(main_path);

% Open a window
[screen_ptr screen_rect] = Screen('OpenWindow', screen_number, 0, [], 32, 2);

% Handy to know middle of screen
screen_middle_x = round(screen_rect(3)/2);
screen_middle_y = round(screen_rect(4)/2);

% Hide the cursor
HideCursor;

% Give the display a moment to recover from the change of display mode when
% opening a window. It takes some monitors and LCD scan converters a few
% seconds to resync.
WaitSecs(2);

% Font setup
Screen('TextFont', screen_ptr, font);
Screen('TextSize', screen_ptr, font_size);
Screen('TextStyle', screen_ptr, font_style);

% Open a sound channel for beep
Snd('Open');

% Get flip interval in case you need it for timing
% Priority(max_priority_level);
ifi = Screen('GetFlipInterval', screen_ptr, 200);
% Priority(0);

% How many frames to leave up cue and stimulus
% Better than using ms, because you know exactly what kind of timing
% you're getting.
num_cue_frames = round(cue_time / ifi);
num_stimulus_frames = round(stim_time / ifi);

% File setup ------------------------------------------------------

% Figure out subject number (+1 of last used)
% Can do same thing with multiple conditions
done = 0;
sub_num = 1;
while ~done
tmp_file_name = [out_file_prefix num2str(sub_num) data_file_postfix];
if exist(tmp_file_name) == 0
done = 1;
else
sub_num = sub_num + 1;
end
end

% Open output file
out_file_ptr = fopen(tmp_file_name, 'w');

% Write to header file. One per experiment
tmp_file_name = [out_header_prefix data_file_postfix];

if exist(tmp_file_name) == 0
out_header_ptr = fopen(tmp_file_name, 'w');
fprintf(out_header_ptr, 'mds_similarity_experiment\n');
fprintf(out_header_ptr, 'sub_num block trial left_graph# right_graph# response_number
reaction_time real_trial\n');
fclose(out_header_ptr);

end




for warm_real=0:1

switch warm_real
case 0 %warm-up trials
num_stimuli = 4;
exp = 4;
numNodes = 6;
stimuli_folder = 'Stimuli/Exp4/'; % Where are the warm-up stim, and what are
they called
otherwise %real trials
num_stimuli = 5;
exp = 5;
numNodes = 6;
stimuli_folder = 'Stimuli/Exp5/'; % Where are the real stim, and what are they
called
end

%reset font to default for instructions (it was Courier for answer
%scale at top of screen
Screen('TextFont', screen_ptr, font);



num_trials = num_stimuli; % Number of trials/block, assumes = number of stimuli

%reset random number seeds so same graphs are picked each time
seed_rand = 0;
seed_randn = 0;
rand('state', seed_rand);
randn('state', seed_randn);


%initialize array for 'names' of graph files
grNames=zeros(num_stimuli, 6);
grDuplicates=zeros(num_stimuli);
%randomly generate names of files
%check as you go that there are no duplicates
for i=1:num_stimuli

%try 10 times to not find a duplicate
for dup=1:10

%generate random number for degrees, perturbation,
% block, and session
pertRnd=rem(fix(100*rand),3); %generates 0, 1, or 2
blockRnd=rem(fix(100*rand),3)+1; %generates 1, 2, or 3
sessRnd=rem(fix(100*rand),3)+1; %generates 1, 2, or 3
degRndTemp=rem(fix(10*rand),6)+1; %generates 1,2,3,4,5, or 6


%create number for easy checking for duplicates
expDigit=(10^5)*exp;
nodeDigit=(10^4)*numNodes;
degDigit=(10^3)*degRndTemp;
pertDigit=(10^2)*pertRnd;
blockDigit=(10^1)*blockRnd;
sessDigit=(10^0)*sessRnd;
quickCode=expDigit+nodeDigit+degDigit+pertDigit+blockDigit+sessDigit

%search for a duplicate
duplicate=0; %0 means no duplicate
for k=1:(num_stimuli-1)
if quickCode==grDuplicates(k)
duplicate=1;
break
end
end
%was there a duplicate found?
if duplicate
duplicate
quickcode
else
break
end
end %end of 'for dup' loop that looks for duplicates

%translate degRnd into
switch degRndTemp
case 1
degRnd=0;
case 2
degRnd=60;
case 3
degRnd=120;
case 4
degRnd=180;
case 5
degRnd=240;
otherwise
degRnd=300;
end

%store specs for graph name in the array grNames
grNames(i,1)=exp;
grNames(i,2)=numNodes;
grNames(i,3)=degRnd;
grNames(i,4)=pertRnd;
grNames(i,5)=blockRnd;
grNames(i,6)=sessRnd;
end %for loop that generates all the random graphs to read in


% Store all potential images as textures now to save time later
clear stimuli
%next_slot=1;
for stim_num = 1:num_stimuli
clear stimulus

%build up the name of the jpg file of the array of random graph names
%experiment number
es=[ 'exp' int2str(grNames(stim_num,1)) '_'];
%graph size (numNodes = 4,6, or 8)
ns=[ 'n' int2str(grNames(stim_num,2)) '_'];
%rotation (deg = 0,60,120,180,240, or 300)
rs=[ 'r' int2str(grNames(stim_num,3)) '_'];
%perturbation 0=0 nodes, 1=half, 2=all, 3=random]
ps=[ 'p' int2str(grNames(stim_num,4)) '_'];
%block (a block of 57 graphs puzzles)
bs=[ 'b' int2str(grNames(stim_num,5)) '_'];
%session (session consists of a collection of blocks)
ss=[ 's' int2str(grNames(stim_num,6))];
%file format
fs='.jpg';


%concatenate them all to create a file name
grfile=[es ns rs ps bs ss fs]
stimulus = imread([stimuli_folder grfile]);
grLBlank=stimulus(1:768, 1:130, :); %grab left blank area
stimuli(:, :, :, stim_num) = stimulus(1:768, 131:506, :);
grMiddle=stimulus(1:768, 507:518, :); %grab the middle including midline

end


% Pair all graphs together
tot_pairings=round(((num_stimuli*num_stimuli)-num_stimuli)/2);
%this will always be an integer, but just rounding as a precaution
Pairings=zeros(tot_pairings,2);
next_slot=1;
for i=1:num_stimuli
for j=1:num_stimuli
if i<j
Pairings(next_slot,1)=i;
Pairings(next_slot,2)=j;
next_slot=next_slot+1;
end
end
end


Pairings

seed_rand = sum(round(sum(37*clock)));
seed_randn = sum(round(sum(183*clock)));
rand('state', seed_rand);
randn('state', seed_randn);


%randomly switch order so lowest numbered graph is not
%always displayed on the left side.
for i=1:tot_pairings
switchsides=rem(fix(100*rand),2);
if switchsides==1
i
temp1=Pairings(i,1);
Pairings(i,1)=Pairings(i,2);
Pairings(i,2)=temp1;
end
end

Pairings


%randomly sort pairings array
revs=fix(1000*rand)
for i=1:revs
switch1=rem(fix(100*rand),tot_pairings)+1;
switch2=rem(fix(100*rand),tot_pairings)+1;
temp1=Pairings(switch1,1);
temp2=Pairings(switch1,2);
Pairings(switch1,1)=Pairings(switch2,1);
Pairings(switch1,2)=Pairings(switch2,2);
Pairings(switch2,1)=temp1;
Pairings(switch2,2)=temp2;
end

Pairings


% Run the warm-up trials -----------------------------------------------

% Startup

switch warm_real
case 0 %warm-up trials

% Give instructions & wait for keypress
Screen('FillRect', screen_ptr, white);
instructions_file_ptr = fopen([text_folder instructions_file]);
line_num = 0;
while 1
tmp_text = fgetl(instructions_file_ptr);
if ~ischar(tmp_text)
break
else
Screen('DrawText', screen_ptr, tmp_text, text_line_left_margin,
text_line_top_margin + line_num*text_line_height, black);
end
line_num = line_num + 1;
end
fclose(instructions_file_ptr);
Screen('Flip', screen_ptr);
WaitForKeyPress(input_device); % Defined as a function below

WaitSecs(0.5);


% Give warm-up instructions & wait for keypress
Screen('FillRect', screen_ptr, white);
instructions_file_ptr = fopen([text_folder warm_up_file]);
line_num = 0;
while 1
tmp_text = fgetl(instructions_file_ptr);
if ~ischar(tmp_text)
break
else
Screen('DrawText', screen_ptr, tmp_text, text_line_left_margin,
text_line_top_margin + line_num*text_line_height, black);
end
line_num = line_num + 1;
end
fclose(instructions_file_ptr);
Screen('Flip', screen_ptr);
WaitForKeyPress(input_device); % Defined as a function below

otherwise %real trials (only put up reminder instructions)

% Give instructions & wait for keypress
Screen('FillRect', screen_ptr, white);
instructions_file_ptr = fopen([text_folder more_instructions_file]);
line_num = 0;
while 1
tmp_text = fgetl(instructions_file_ptr);
if ~ischar(tmp_text)
break
else
Screen('DrawText', screen_ptr, tmp_text, text_line_left_margin,
text_line_top_margin + line_num*text_line_height, black);
end
line_num = line_num + 1;
end
fclose(instructions_file_ptr);
Screen('Flip', screen_ptr);
WaitForKeyPress(input_device); % Defined as a function below

%WaitSecs(0.5);


end



Screen(screen_ptr, 'TextFont', 'Courier');

% Run the blocks
[feedback_vbl feedback_sos feedback_timestamp] = Screen('Flip', screen_ptr); % To clear
the screen and start things off

for block = 1:num_blocks

% Run a trial
num_trials=tot_pairings;
for trial = 1:num_trials

% Set priority to maximum
% Priority(max_priority_level);

% Clear & synch the screen

Screen('PutImage', screen_ptr, stimuli(:, :, :, Pairings(trial,1)), [130 0 506 768]);
Screen('PutImage', screen_ptr, stimuli(:, :, :, Pairings(trial,2)), [518 0 894 768]);

%Draw midline
Screen('DrawLine',screen_ptr,black,512,100,512,668,3);

% Draw scale of responses
Screen(screen_ptr,'DrawText', 'a s d f j k l ;',250,30);
Screen(screen_ptr,'DrawText', '1 2 3 4 5 6 7 8',250,50);
Screen(screen_ptr,'DrawText', 'least similar most similar',200,70);

% Tell PTB we're done drawing
Screen('DrawingFinished', screen_ptr);

% Draw the stimulus
[stim_vbl stim_sos stim_timestamp] = Screen('Flip', screen_ptr, feedback_vbl +
feedback_time); % Add a bit of delay for good measure

% Get the response
% Not permitted before stimulus disapears.
% To change this, simply comment out the 'clear the screen' flip
% line. That is, don't wait until screen gets cleared to collect the
% response.
key_down = 0;
while ~key_down
WaitSecs(0.001);
[key_down time_secs key_code] = KbCheck(input_device);

if key_down
resp_key = KbName(key_code);

if strcmp(resp_key, 'a') == 1
resp_num = 1;
elseif strcmp(resp_key, 's') == 1
resp_num = 2;
elseif strcmp(resp_key, 'd') == 1
resp_num = 3;
elseif strcmp(resp_key, 'f') == 1
resp_num = 4;
elseif strcmp(resp_key, 'j') == 1
resp_num = 5;
elseif strcmp(resp_key, 'k') == 1
resp_num = 6;
elseif strcmp(resp_key, 'l') == 1
resp_num = 7;
elseif strcmp(resp_key, ';:') == 1
resp_num = 8;
else
key_down = 0;
end

end
end

[feedback_vbl feedback_sos feedback_timestamp] = Screen('Flip', screen_ptr);

rt = time_secs - stim_vbl;

% Set priority back to low (good to do once in a while)
% Priority(min_priority_level);

% Store data
fprintf(out_file_ptr, '%d %d %d %d %d %d %g %d\n', sub_num, block, trial,
Pairings(trial,1), Pairings(trial,2), resp_num, rt, warm_real);

end
end

end %for warm_real=0:1



% Debriefing ----------------------------------------------------------

Screen('TextFont', screen_ptr, font);
Screen('FillRect', screen_ptr, white);
debriefing_file_ptr = fopen([text_folder debriefing_file]);
line_num = 0;
while 1
tmp_text = fgetl(debriefing_file_ptr);
if ~ischar(tmp_text)
break
else
Screen('DrawText', screen_ptr, tmp_text, text_line_left_margin,
text_line_top_margin + line_num*text_line_height, black);
end
line_num = line_num + 1;
end
fclose(debriefing_file_ptr);
Screen('Flip', screen_ptr);
WaitForKeyPress(input_device); % Defined as a function below
WaitSecs(0.5);



% Closing stuff ----------------------------------------------------

ShowCursor; % Show the cursor
fclose(out_file_ptr); % Close the output file
Screen('CloseAll'); % Close the psychtoolbox screens
Priority(min_priority_level);
Snd('Close'); % Close the sound channel

catch

% This "catch" section executes in case of an error in the "try" section
% above. Importantly, it closes the onscreen window if its open.
%ShowCursor;
%fclose(out_file_ptr);
%Screen('CloseAll');
%Priority(min_priority_level);
%Snd('Close'); % Close the sound channel
%rethrow(lasterror);

end %try..catch

% ---------------------------------------------------------
% Local functions
% ---------------------------------------------------------
function WaitForKeyPress(input_device)

key_down = 0;
while ~key_down
WaitSecs(0.001);
[key_down secs keyCode]= KbCheck(input_device);
end