I can't get my error-bar plot to work

10 次查看(过去 30 天)
I can't get this code to work:
function [fitresult, gof] = Linearized_plot_uncertainties(inverse_square_radius, time,er)
%%Fit: 'Linearized Fit'.
[xData, yData] = prepareCurveData( inverse_square_radius, time );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.
figure( 'Name', 'Linearized Fit' );
h = errorbar(fitresult,'b', xData, yData,'.k',er); %THIS LINE gives me the error message
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
legend( h, 'time vs. inverse_square_radius', 'Linearized Fit', 'Location', 'NorthEast' );
% Label axes
xlabel inverse_square_radius
ylabel time
grid on
hold on;
axis([0 10.5e5 0 40.5]);
I'm getting the following error message:
Error using errorbar (line 66)
Not enough input arguments.
Error in Linearized_plot_uncertainties (line 13)
h = errorbar(fitresult,'b', xData, yData,'.k',er);
When I replace the line with this (removing customization):
h = errorbar(fitresult, xData, yData,er);
I'm getting a different error message:
Error using errorbar (line 76)
Input arguments must be numeric or objects which can be converted to double.
Error in Linearized_plot_uncertainties (line 14)
h = errorbar(fitresult, xData, yData,er);

采纳的回答

dpb
dpb 2018-7-10
编辑:dpb 2018-7-10
h = errorbar(fitresult,'b', xData, yData,'.k',er); %THIS LINE gives me the error message
As it rightfully should; matlab/ref/errorbar doesn't indicate it supports a fit object in the argument list; the syntax is given as one of
...
errorbar(x,y,err)
errorbar(x,y,neg,pos)
...
errorbar(___,linespec)
errorbar(___,Name,Value)
...
It's that you've got an object handle in the location for a coordinate array that is the reason for the various messages depending upon just which perturbation you chose.
hEB=errorbar(xData,yData,er,'.-b', ...
'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
should come reasonably close to the result you were looking for.
  2 个评论
Nicholas Pfaff
Nicholas Pfaff 2018-7-11
Modified my code slightly using your suggestion and it works:
function [fitresult, gof] = Linearized_plot_uncertainties(inverse_square_radius, time,er)
%%Fit: 'Linearized Fit'.
[xData, yData] = prepareCurveData( inverse_square_radius, time );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.
figure( 'Name', 'Linearized Fit' );
h = plot( fitresult,'b', xData, yData, '.k' );
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
hold on;
hEB=errorbar(xData,yData,er,'.k', 'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
legend( h, 'time vs. inverse_square_radius', 'Linearized Fit', 'Location', 'NorthEast' );
% Label axes
xlabel inverse_square_radius
ylabel time
grid on
hold on;
axis([0 10.5e5 0 40.5]);
Thanks!
dpb
dpb 2018-7-11
编辑:dpb 2018-7-11
No problem..one has to read doc carefully to ensure match the signature prototype. It's become far more complicated as TMW tries to overload many functions with the newfangled OO stuff; some they've gotten to, others they haven't (at least yet).
'Tis a nightmare they're creating, however, methinks, going forward as shown in another recent similar case-- Answer_328137 Particularly note the follow-up comments to IA on the difficulty in parsing input string for plot; same kind of logic would have to be added to errorbar and all the other special graphics to support fit objects similarly. Whether this is in the long range vision or not is unknown, but complexity reigneth supreme. I can understand the thinking but I wonder if it really is the best solution in the long run.

请先登录,再进行评论。

更多回答(1 个)

Steve Slana
Steve Slana 2018-7-10
I think you might need to put your xdata and ydata first as there aren't any indentifiers associated to those parameters?

标签

Community Treasure Hunt

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

Start Hunting!

Translated by