xData = level(1:100)';
yData = [trialData(1:100).correct]';

% no NaNs

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

figure( 'Name', 'Example Psychophysical function fitting' );
subplot(5,1,1)
h = plot( fitresult, xData, yData ); title('First Half')   
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

% fit for second half

xData = level(101:200)';
yData = [trialData(101:200).correct]';

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

subplot(5,1,2)
h = plot( fitresult, xData, yData ); title('Second Half') 
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

xData = level(201:300)';
yData = [trialData(101:200).correct]';

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

subplot(5,1,3)
h = plot( fitresult, xData, yData ); title('Second Half') 
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

xData = level(301:400)';
yData = [trialData(101:200).correct]';

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

subplot(5,1,4)
h = plot( fitresult, xData, yData ); title('Second Half') 
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on

xData = level(401:500)';
yData = [trialData(101:200).correct]';

ft = fittype( '0.5+(1-0.5-l)./(1+exp(-(x-alpha)/beta))', 'independent', 'x', 'dependent', 'y' );

opts = fitoptions( ft );    
opts.Display = 'Off';    
opts.Lower = [-Inf 0 0];    
opts.StartPoint = [0.132903481469417 0.762038603323984 0];    
opts.Upper = [Inf 1 1];

[fitresult, gof] = fit( xData, yData, ft, opts );

subplot(5,1,5)
h = plot( fitresult, xData, yData ); title('Second Half') 
legend( h, 'responses vs. sample positions', 'fit', 'Location', 'NorthEast' );    
% Label axes    
xlabel( 'stimulus intensity' );    
ylabel( 'Probability of correct response' );    
grid on