How to create legend for two error bar plots

The only problem with my code is, that the legend line gives error messages and doesn't show up in the graph. I want a legend with 4 elements (The data of graph 1, the fit of graph 1, the data of graph 2, the fit of graph 2). I don't want the error bars to show in the legend. My code:
function [fitresult, gof, goft] = Linearized_plot_both(inverse_square_radius,time,er,inverse_square_radiust,timet,ert)
%%Fit: 'Linearized Fit'.
[xData, yData] = prepareCurveData( inverse_square_radius, time );
[xDatat, yDatat] = prepareCurveData( inverse_square_radiust, timet );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
[fitresultt, goft] = fit( xDatat, yDatat, ft );
% Plot fit with data.
figure( 'Name', 'Comparing Linearized Fit' );
x= linspace(0,11e5);
y=(fitresult.p1).*x + (fitresult.p2);
h=plot(xData,yData,'.k',x,y,'b');
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
hold on;
axis([0 10.5e5 0 41.5]);
grid on;
hEB=errorbar(xData,yData,er,'.k', 'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
xt= linspace(0,11e5);
yt=(fitresultt.p1).*x + (fitresultt.p2);
ht=plot(xDatat,yDatat,'.k',xt,yt,'r');
ht(1).MarkerSize = 12;
ht(2).LineWidth = 1;
hold on;
hEBt=errorbar(xDatat,yDatat,ert,'.k', 'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
legend([h,ht],'time vs. inverse_square_radius (Data)','Linearized Fit (Data)','time vs. inverse_square_radius (Theory)','Linearized Fit (Theory)');
% Label axes
xlabel inverse_square_radius
ylabel time
The produced graph:
Way I want my legend to look (just for both graphs):
%

5 Comments

I don't see anything obvious but can't run your code nor duplicate the intended effect without a fair effort. How about posting a simple test case with included data that illustrates the issue; perhaps even in doing that you'll uncover the problem...
My Data:
inverse_square_radius =
1.0e+06 *
1.0000
0.4444
0.2500
0.1600
0.1111
0.0816
0.0625
0.0494
0.0400
0.0331
0.0278
0.0237
0.0204
0.0178
0.0156
0.0138
0.0123
0.0111
0.0100
0.0091
inverse_square_radiust =
1.0e+06 *
1.0000
0.4444
0.2500
0.1600
0.1111
0.0816
0.0625
0.0494
0.0400
0.0331
0.0278
0.0237
0.0204
0.0178
0.0156
0.0138
0.0123
0.0111
0.0100
0.0091
time =
39.1100
16.9700
9.6100
7.0400
4.8100
3.5100
2.6600
2.3000
1.9100
1.6000
1.4800
1.3200
1.0200
0.9400
0.8400
0.8200
0.7700
0.7200
0.6600
0.6300
timet =
31.6000
14.0400
7.9000
5.0600
3.5100
2.5800
1.9800
1.5600
1.2600
1.0400
0.8800
0.7500
0.6400
0.5600
0.4900
0.4400
0.3900
0.3500
0.3200
0.2900
er =
0.6700
0.4600
0.2400
0.2800
0.2200
0.3700
0.3400
0.2000
0.1700
0.1900
0.2400
0.2100
0.1800
0.1900
0.2300
0.1800
0.1700
0.1800
0.1700
0.1800
ert =
0.9500
0.4200
0.2400
0.1500
0.1100
0.0800
0.0600
0.0500
0.0400
0.0300
0.0300
0.0200
0.0200
0.0200
0.0100
0.0100
0.0100
0.0100
0.0100
0.0100
This is the error message:
Operands to the || and && operators must be convertible to logical scalar values.
Error in legend (line 162)
all(isgraphics(args{1})) % legend(children,strings,...)
Error in Linearized_plot_both (line 34)
legend([h,ht],'time vs. inverse_square_radius (Data)','Linearized Fit (Data)','time vs.
inverse_square_radius (Theory)','Linearized Fit (Theory)');
Please attach a mat or text file with the variables. Help us help you...

Sign in to comment.

 Accepted Answer

dpb
dpb on 12 Jul 2018
Edited: dpb on 12 Jul 2018
h and ht are column vectors of handles and you've concatenated them into a 2x2 array instead of a vector...it's trying to put those together inside legend that's failing.
Use ; instead of , --
legend([h;ht],'time vs. inverse_square_radius (Data)','Linearized Fit (Data)','time vs. inverse_square_radius (Theory)','Linearized Fit (Theory)');
You'll probably cover up most of the plot area with the length of the text, but that's another problem... :)

2 Comments

dpb
dpb on 12 Jul 2018
Edited: dpb on 12 Jul 2018
legend documents subset handles input must be a vector but seems rude for it to fail unceremoniously; it couldn't cost anything for it to use (:) on the argument inside to ensure is a vector or, failing that, inform of the what the error is during input check...
I didn't catch it just reading the code; set breakpoint in editor to see what you ended up with (hence the request for sample or data to run yours; almost certainly if had tried to emulate I'd have missed the crucial mistake and not found the problem).
ADDENDUM
I did submit enhancement SRQ to either add informative error or, it turns out that one can modify the failing line by the (:) and it will then accept the array of handles. I made the patch and recompiled the p-code and with that the existing code runs to completion with the expected result. No big deal but even an informative error would be better than the crash inside TMW-supplied function with no guidance as to why.

Sign in to comment.

More Answers (0)

Asked:

on 11 Jul 2018

Edited:

dpb
on 12 Jul 2018

Community Treasure Hunt

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

Start Hunting!