Why is my legend for the calculated bode plot appearing twice?
3 views (last 30 days)
Show older comments
I have written a small script that inputs a text file of my experimental bode plot measurements and then using tf(), I created the theoretical plot. Using semilogx(), I plotted the experimental data. Then using hold on; and bodeplot I plotted the theoretical data on the same graph.
I'm having two issues, the first is that with the current code I have written, I cannot get the legend to read Calculated and Measured. Instead, two legend boxes appear. One with both calculated and measured and the other with only measured. Why is calculated appearing twice? How can I achieve one legend box with both plot labels?
My second problem is with the bodeoptions. It will not accept any XLabel, YLable, or Title input arguments. Why is this happening?
A snippet of my code:
figure(1);
opts = bodeoptions('cstprefs');
opts.PhaseVisible = 'off';
opts.FreqUnits = 'Hz';
opts.FreqScale = 'linear';
opts.Grid = 'on';
semilogx(butterWorthLP(1:3:end), butterWorthLP(2:3:end), 'b','linewidth',2)
legend('Measured Butterworth Filter','location','southwest');
hold on;
h = bodeplot(sys2,opts,'b-.');
legend ('Calculated Butterworth Filter','location','southwest');
f = findobj(gcf,'type','line');
set(f,'linewidth',2);
title('Butterworth Filter: Experimental vs. Theoretic');
0 Comments
Accepted Answer
dpb
on 5 Nov 2015
Edited: dpb
on 5 Nov 2015
...
semilogx(butterWorthLP(1:3:end), butterWorthLP(2:3:end), 'b','linewidth',2)
hold on;
h = bodeplot(sys2,opts,'b-.');
legend('Measured Butterworth Filter','Calculated Butterworth Filter','location','southwest');
You had two separate calls to legend making two objects, not a call with two labels.
Would have to see the code and error on the options; first guess is that you're trying something like
p=bodeoptions;
p.Title='Your Plot Title';
but Title is yet another structure, not the title data; that's
p.Title.String='Your Plot Title';
More Answers (1)
dpb
on 6 Nov 2015
Edited: dpb
on 9 Nov 2015
OK, I think I see where the problem lies -- TMW has fouled up things again with the inconsistency they've introduced with objects vis a vis previous/existing code. I don't have either toolbox so can't test for absolute certain, but my best guess from reading the published doc is that bodeplot and the rest of the family return a "plot handle", NOT the line handle(s) that are what plot, semilog? etc., etc., return. Consequently, the symmetry of where to find those line handles which legend uses is broken and it fails when one uses the two types of plots together on the same axes--a big "Woops! We never thought of THAT!!!" moment.
Unfortunately also for ease of use, the plot line handles are not one of the available/exposed properties you can access through the provided interface, either. Hence, it appears that "handle diving" to retrieve that handle that you can subsequently pass to legend (along with the handle from the semilogx call) using the
legend(hL,'string1','string2',...)
syntax form where hL is the above-described vector of line handles.
It's a pity Matlab is becoming so disjointed as they switch horses in midstream of the design philosophy as well as the code memory bloat and performance hit all this causes. We can only hope in another 10 yr or so it'll have morphed sufficiently to a new product that has gotten most of this cleaned up by then...
Try rearranging as follows and see if nirvana follows...
hL1=semilogx(measuredX, measuredY, 'b','linewidth',2);
hold on;
h = bodeplot(sys2,opts,'b-.');
hL = findobj(gcf,'type','line');
legend(hL,'Measured Butterworth Filter', ...
'Calculated Butterworth Filter', ...
'location','southwest');
set(hL,'linewidth',2);
NB: I switched your variable f to hL for "handles, Line". Also, hL(1) should be == hL1 just as a sanity check that the strings are in the proper order; not sure if the findobj call will return them in the order created (FIFO) or the reverse in order (LIFO).
0 Comments
See Also
Categories
Find more on Get Started with Control System Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!