Why I am getting error message when I try to define line specs of a plot?

17 views (last 30 days)
Hello
I am trying to fit a curve to a data and create a plot. The code creates the plot, but when I define the line width, it gives error message;
f = fittype('a + b*log(x)',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a','b'});
fit1 = fit( time1, strain1, f )
plot(fit1,'k-','LineWidth',2)
The error message I got is below;
fit1 =
General model:
fit1(x) = a + b*log(x)
Coefficients (with 95% confidence bounds):
a = 0.04434 (0.04336, 0.04532)
b = 0.004645 (0.004399, 0.004891)
Index exceeds matrix dimensions.
Error in cfit/plot (line 66)
ydata = ydata(idx);
It does not give any error message if I do not define 'LineWidth'.
Waiting for your kind help.
Regards

Accepted Answer

Walter Roberson
Walter Roberson on 8 Apr 2019
plot() for cfit objects is not the generalized plot function. It does not accept LineWidth parameters.
You should proceed like
h = plot(fit1, 'k-');
set(h, 'LineWidth', 2);

More Answers (2)

A. Sawas
A. Sawas on 8 Apr 2019
This will solve the problem.
p = plot(fit1,'k-');
p.LineWidth = 2;

TADA
TADA on 8 Apr 2019
You are using an overload of plot that accepts a cfit object
this overload doesn't support all the functionality of the regular plot function
you can calculate the fit data and plot that, or you can take a handle of the curve and fix that later:
time1 = (1:100)';
strain1 = log(time1);
f = fittype('a + b*log(x)',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a','b'});
fit1 = fit( time1, strain1, f );
% calculate the fit vector and plot numbers
y = feval(fit1, time1);
plot(time1, y, 'k-', 'LineWidth', 2);
% manipulate the curve handle after plotting
h = plot(fit1,'k-');
h.LineWidth = 2;

Community Treasure Hunt

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

Start Hunting!