MATLAB: Plot vectors must be same length

MATLAB

fileName='ExtractedDataandGraphs.xlsx';
data=xlsread(fileName);
t=1.5; w=1.27; l=4.49;
A=w*t;
time=data(1:24,2);
u=0.449;
F=data(1:24,3);
sigma=F/A;
fig0=figure;
plot(time,F); xlabel('time (s)'); ylabel('Force (MPa)');
fig1=figure;
h11=subplot(3,1,1);
plot(time, sigma);
xlabel('time (s)'); ylabel('stress (MPa)');
title('Cartilage');
%removing ramp
[maxS, idxMax]=max(sigma);
t1=time(idxMax:end);
t1=t1-t1(1);
sigma1=sigma(idxMax:end);
fig2=figure;
h12=subplot(3,1,2);
plot(t1,sigma1); legend('sigma from data');
xlabel('time (s)'); ylabel('sigma (MPa)');
%variable creation
xdata=t1;
ydata=sigma1;
%fitting function
fun2fit=@(params, time) params(1)*exp(-params(2)*t1)+params(3);
%guess parameters
guess=[100,40,10];
funGuess=fun2fit(guess,time);
plot(time,funGuess);
I have tried everything and looked at so many other forums. I cannot figure out what is wrong with this and why ot wont complete the last plot function.

Best Answer

K - the error is because of this line
fun2fit=@(params, time) params(1)*exp(-params(2)*t1)+params(3);
Note how the second input parameter is time but you don't use it in the function. Rather you use t1 which is a 13x1 array. So the output of
funGuess=fun2fit(guess,time);
is a 13x1 array which when trying to plot it against the 24x1 time array at
plot(time,funGuess);
will lead to the above error. Just change your anonymous function to use time instead of t1
fun2fit=@(params, time) params(1)*exp(-params(2)*time)+params(3);