try
    clear all;
    pixelSize=0.0353; %degree/pixel
    LCDframeRate = 60; %Hz
    cd C:\Users\user\Desktop\Matlab學習\demo\psychtoolbox_demo\class07\Practice_Class07 % current path

    % general present parameters 
    ITI=4; %second , the duration between stimuli
    dur1=1.2; %second the duration of one stimuli   %% fixation=ITI-dur1-black-dur2
    black=1;
    dur2=1.5;
    nInitWait=9; %second initial waiting time (put some stimuli to get subjects's stable state) 

    %get info
      subNo = input('What is your subject number?', 's');    
    %set up labels
      MorphObject = {'subNo','trial','objnumber', 'objname', 'objtype' 'response time', 'response','new','is_correct'};
  %% Define filenames of input files and result file:
      datafilename = strcat('MorphObject_',num2str(subNo),'.dat'); % name of data file to write to
  %   studyfilename = 'studylist.txt';                             % study list
      testfilename  = 'MorphObject_test.txt';                      % test list
      
    
      
       % read list of conditions/stimulus images -- textread() is a matlab function
        % objnumber  arbitrary number of stimulus
        % objname    stimulus filename
        % objtype    
        [ objnumber, objname, objtype ] = textread(testfilename,'%d %s %s');
        
         % Randomize order of list
        ntrials=length(objnumber);         % get number of trials
        randomorder=randperm(ntrials);     % randperm() is a matlab function
        objnumber=objnumber(randomorder);  % need to randomize each list!
        objname=objname(randomorder);      %
        objtype=objtype(randomorder);      %
     
   % read data
    cd C:\Users\user\Desktop\Matlab學習\demo\psychtoolbox_demo\class07\Practice_Class07\Stimuli2
    
   % bgImage=zeros(size(testImage(:,:,1)));
    bgImage=zeros(1,1);

    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     %%% Run Experiment  %%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

     % Eye Catching 
    [win1 rect]=Screen(0,'OpenWindow',0); 
    HideCursor % hide mouse 
    centerx=rect(3)/2; % put image on the screen center
    centery=rect(4)/2; 
    rightx=centerx+rect(3)/4;
    leftx=centerx-rect(3)/4;

    %Screen(win1,'TextFont','arial'); 
    %Screen(win1,'TextSize',14); 
    Screen(win1,'DrawText','實驗正式開始',centerx-100,centery,255); %255表示白字
    Screen(win1, 'Flip');
    KbWait;
    %t0=getsecs; % start to get time
    %t0=KbWait; % demo use Waits until any key is down and returns the time (GetSecs)
    Screen(win1,'PutImage',bgImage);   
    Screen(win1,'FillOval',255,[centerx-5,centery-5,centerx+5,centery+5 ]); % draw a fixation 
    Screen(win1, 'Flip');

    [key_is_down, secs, key_code] = KbCheck;
    % initWaite
    t1=GetSecs; % start to get time
    for h = 1:ntrials  %  initwait usually 2-3TR (start of each run)
       if randomorder(1,h) < 10
           fname1=['object','1','0',num2str(randomorder(1,h)),'.jpg'];
       else 
           fname1=['object','1',num2str(randomorder(1,h)),'.jpg'];
       end 
       testImage=imread(fname1,'jpg');
       Screen(win1,'PutImage',testImage); 
       Screen(win1, 'Flip');
       WaitSecs(1.2);
       Screen(win1,'PutImage',bgImage);   
       Screen(win1, 'Flip');
       WaitSecs(1);
       t2=GetSecs;  
       if randomorder(1,h) < 10
           fname2=['object','2','0',num2str(randomorder(1,h)),'.jpg'];
       else 
           fname2=['object','2',num2str(randomorder(1,h)),'.jpg'];
       end 
       testImage2=imread(fname2,'jpg')
       list3=randperm(2);
       if (list3(1,1)==1)
           Screen(win1,'PutImage',testImage2,[rightx-250,centery-250,rightx+250,centery+250]);
           Screen(win1,'PutImage',testImage,[leftx-250,centery-250,leftx+250,centery+250]);
           Screen(win1, 'Flip');
       else 
           Screen(win1,'PutImage',testImage,[rightx-250,centery-250,rightx+250,centery+250]);
           Screen(win1,'PutImage',testImage2,[leftx-250,centery-250,leftx+250,centery+250]);
           Screen(win1, 'Flip');
       end
       KbWait;                  
           %tests your reaction time
                  while 1  %表示有按鍵 
                      [key_is_down, secs, key_code] = KbCheck;
                     if(key_is_down)
                          break
                     end
                  end
                  response_time = secs - t2;
                  disp(['Your time was ', num2str(response_time)]);
                
          % find out what the name of the key is
                  [key_is_down, secs, key_code] = KbCheck;
                  name = KbName(key_code);
                  disp(name);
                  
               
             Screen(win1,'PutImage',bgImage);   
             Screen(win1, 'Flip');
             WaitSecs(0.5);
   
       while(GetSecs-t2 < dur2)
       end
        
       
       Screen(win1,'PutImage',bgImage);
       Screen(win1,'FillOval',255,[centerx-5,centery-5,centerx+5,centery+5]);  %% 255: black時的fixation為白色,[]表示圓的半徑為5
       Screen(win1, 'Flip');
       while(GetSecs-t2 < ITI) 
       end
       
     
       %record data
         response = name
         new =num2str((list3(1,1)==1)+1) %2=新的在右邊 1=新的在左邊 
         is_correct = (response==new)
         MorphObject (h+1, :) = {subNo, h, objnumber(h), objname(h), objtype(h), response_time, response, new, is_correct};             
    
    end 
   
       fid= fopen(datafilename,'wt')
     % Write h result to file:
            fprintf(fid),'%s %i %i %s %s %i %s %s %i\n', ...
                subNo, ...
                h, ...
                objnumber(h), ...
                char(objname(h)), ...
                objtype(h), ...
                response_time,...
                response, ...
                new,...
                is_correct);
        fclose(fid);
    
     
            
     %data_storage
      %csvwrite([subNo,'.csv'], MorphObject)     
     %xlsFile = strcat('MorphObject_',num2str(subNo),'.xls');
     %[status, message] = xlswrite(xlsFile, MorphObject);
     %dos(['start ' xlsFile]);
    
   
    %Clean up
    Screen(win1,'PutImage',ones(size(bgImage))*0);
    Screen('closeAll');

    
catch 
    %Screen('closeAll');
end

