Plot in child (Matlab) - matlab

Can someone please tell me how does the last plot command work in following script?
close all;
s=tf('s');
sys1 = 5/(s+5);
sys2=exp(-1*s);
G=ss(sys1)*ss(sys2);
opts = bodeoptions('cstprefs');
opts.Grid= 'ON';
% create a figure and get the handle of the figure
figHnd = figure;
bode(G,opts)
% get and display the children handles of the figure
childrenHnd =get(figHnd, 'Children');
% select magnitude plot and plot a line
axes(childrenHnd(3));
hold on;
plot([1 1], [-20 20], 'r')
hold off;
I am trying to add a horizontal line for cut-off frequency to my Bode plot (magnitude diagram) but I can't figure out how to do that. The current code adds a vertical line for me.

The question is about the line
plot([1 1], [-20 20], 'r')
which is a simple plot command. In general, you use
plot(x,y)
here it is the same: the x-vector is [1, 1] and the y-vector is [-20, 20]. So you draw a line from the point (1,-20) to (1,20). The last part (r) only specifies the color, i.e. red. This is exactly what you can see in the bode plot.
To create a horizontal line e.g. from (10^-1, -20) to (10^0, -20) you can similarily draw
plot([10^-1, 10^0], [-20, -20], 'r');
(don't forget to put it within hold on; ... hold off;, so the bode plot isn't erased.

change the last plot code similar to this:
plot([1 10], [-20 -20], 'r')
Refer plot and work on some examples to get an idea of how it works.

Related

Add custom legend without any relation to the graph

I wish to insert a legend that is not related to the graph whatsoever:
figure;
hold on;
plot(0,0,'or');
plot(0,0,'ob');
plot(0,0,'ok');
leg = legend('red','blue','black');
Now I wish to add it to another figure:
figure;
t=linspace(0,10,100);
plot(t,sin(t));
%% ADD THE LEGEND OF PLOT ABOVE
This is how I have solved this problem in the past:
figure
t=linspace(0,10,100);
plot(t,sin(t));
hold on;
h = zeros(3, 1);
h(1) = plot(NaN,NaN,'or');
h(2) = plot(NaN,NaN,'ob');
h(3) = plot(NaN,NaN,'ok');
legend(h, 'red','blue','black');
This will plot the additional points, but because the coordinates are at NaN they will not be visible on the plot itself:
EDIT 26/10/2016: My original answer results in greyed out legend entries in 2016b. The updated code above works, but the answer below is only relevant pre-2016b:
figure
t=linspace(0,10,100);
plot(t,sin(t));
hold on;
h = zeros(3, 1);
h(1) = plot(0,0,'or', 'visible', 'off');
h(2) = plot(0,0,'ob', 'visible', 'off');
h(3) = plot(0,0,'ok', 'visible', 'off');
legend(h, 'red','blue','black');
This will plot the additional points, but they will not be visible on the plot itself.
You can also use copyobj to copy graphics elements from one figure to another if you have a lot of elements, then use set(x, 'visible', 'off') to hide them before showing the legend, but it depends on what your final application is.
Your question is a little unclear. However, the first thing I thought of when reading it was the text function in Matlab.
You can use the text function to add text to a Matlab figure. It's use is
>> text(x, y, str);
where x and y are the coordinates in the figure where you want to add the text str. You can use the Color option of text for colours and TeX to draw lines or even _. I've gotten very creative with plots using text.
Here's a quick and dirty example of emulating a legend with text
x = 0:pi/20:2*pi;
y = sin(x);
plot(x,y)
axis tight
legend('sin(x)');
text(5.7, 0.75, 'sin(x)');
text(5.1, 0.78, '_____', 'Color', 'blue');
which produces
For this specific case you could use the specific command (noted by #Hoki in the comments).
ht = text(5, 0.5, {'{\color{red} o } Red', '{\color{blue} o } Blue', '{\color{black} o } Black'}, 'EdgeColor', 'k');
to produce
by retrieving the handle to the text object it becomes trivial to copy it to a new figure, copyobj(ht, newfig). [1]

Why is the actual value of an axis position different than the derived value in MATLAB suplot figure?

I've got a figure with two subplots being displayed. I want to get the axis position of each plot so I am using the figure's children information:
subplot(211)
% Plot stuff here
...
subplot(212)
% Plot stuff here
...
% Get Axes Position information
f =gcf;
% Bottom Plot Axis
disp(f.Children(2).Position)
% Top Plot Axis
disp(f.Children(4).Position)
The console displays:
Bottom plot:
0.1300 0.1100 0.7750 0.3412
Top plot:
0.1300 0.5838 0.7750 0.3412
This seems correct given all else. When I run the plot to generate the figure and then inspect the axes' information via 'Show Plot Tools and Dock Figure->Inspector: matlab.graphics.axis.Axes' option the width value for each axis's position is not 0.7750. The other position values are as listed but the width is different for both the top and bottom plots. The top becomes [0.13 0.5838 0.682 0.341] and the bottom becomes [0.13 0.11 0.67 0.341].
Does anyone know why this is and how to get the "real" position values and not the incorrect displayed/printed ones?
Useful info: MATLAB R2014b
EDIT UPDATE:
Here is a MWE that produces the behavior.
clear all; close all; clc;
figure;
subplot(211)
hold on
x1 = [ -1 -0.998 -0.996 -0.994 -0.992];
y = [0.000324249267578125 -0.000370025634765625 -3.4332275390625e-005 -0.000186920166015625 -0.000110626220703125];
plot(x1, y, '-', 'MarkerSize', 10)
set(gca, 'FontName', 'Interstate-Light', 'FontSize', 7)
set(gca, 'XTickLabel', [])
set(gca, 'XLabel', [])
grid on
ylabel('Frequency Error (Hz)', 'FontName', 'Interstate-Bold')
legend({'FE'}, 'Location', 'NorthEastOutside', 'FontName', 'Interstate-Light', 'Box', 'off')
subplot(212)
hold on
x1 = [ -1 -0.998 -0.996 -0.994 -0.992];
y = [-0.010614013299346 -0.0417663566768169 0.0235949698835611 -0.0502893067896366 0.0316442884504795];
plot(x1, y, '-', 'MarkerSize', 10)
% Overide X-axis ticks to align with data
XTick = unique(x1);
grid on;
xlabel('Time (s)', 'FontName', 'Interstate-Bold')
ylabel('Frequency Error (Hz)', 'FontName', 'Interstate-Bold')
set(gca, 'FontName', 'Interstate-Light', 'FontSize', 7)
legend({'RFE'}, 'Location', 'NorthEastOutside', 'FontName', 'Interstate-Light', 'Box', 'off')
% Get figure object
f = gcf;
% Set fontsize to 16 for readability in final pdf
set(findall(f, '-property','FontSize'),'FontSize',16)
f.PaperUnits = 'inches';
f.PaperPosition = [0 0 20 14];
f.PaperPositionMode = 'manual';
% Get axis position info
disp('Second plot:')
disp(f.Children(2).Position)
disp('First Plot:')
disp(f.Children(4).Position)
I'm working with R2012b and I've been able to reproduce the problem and also (almost) been able to understand what's happening.
I do not know if it is also applicable to more "recent" versin of MatLab.
The problem seems related to the default configuration of the window Show Plot Tools and Dock Figure
With this configuration, actually the position of the two axes (read in the Property editor are different with respect to the "original ones:
original pos ax 1= [0.1300 0.5838 0.7750 0.3412]
new pos ax1=[0.1300 0.629 0.7750 0.268 ]
Nevertheless, if you minimize the Property editor tab and look at the Position of the two axes, they are restored to the "original" one values.
You can, the "slide up" the tab and the position still remain the "original onne.
I'do not know if this is actually an answer to you question; what I can say is that the right position position are the "original" ones.
It could be a sort of bug in the visualization in the default configuratin od the Show Plot Tools and Dock Figure window.
Hope this helps.

In MATLAB, how does one clear the last thing plotted to a figure?

In MATLAB, I plot many different vectors to a figure. Now, what I would like to do is simply undo the last vector that I plotted to that figure, without clearing everything else. How can this be accomplished? Can it be accomplished?
Thanks
Edit:
figure(1); clf(1);
N = 100;
x = randn(1,N);
y = randn(1,N);
z = sin(1:N);
plot(x); hold on;
plot(y,'r');
plot(z,'k');
Now here, I would like to remove the plot z, which was the last plot I made.
If you know before plotting that you want to remove it again later, you can save the handle returned by plot and delete it afterwards.
figure;
h1 = plot([0 1 2], [3 4 5]);
delete(h1);
Try
items = get(gca, 'Children');
delete(items(end));
(or maybe delete(items(1)))
The answer that #groovingandi gives is the best way to generally do it. You can also use FINDALL to find the handle based on the properties of the object:
h = findall(gca, 'type', 'line', 'color', 'k');
delete(h);
This searches the current axes for all line objects (plot produces line objects) that are colored black.
To do this on, say, figure 9, you need to find the axes for figure 9. Figure handles are simply the figure number, so:
ax = findall(9, 'axes');
h = findall(ax, 'type', 'line', 'color', 'k');
delete(h);

bode plot discrepancy

I am plotting the following
Cu4 = tf([1 2], [1 2 6]);
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
phaseCu4 = squeeze(phaseCu4);
semilogx(wout,20*log10(magCu4)),grid;
hold on
bode(Cu4,'r')
I would expect that the semilogx plot would return an identical plot as 'bode'. however, this doesn't seem to be the case. Does anyone know what is going wrong here?
The difference is that you do not specify a frequency vector in your second call to bode so MATLAB chooses a default vector (in your code it had length of 46).
Instead you could try:
bode(Cu4,'r',logspace(-2,7,300))
Compare the plots made by the following code
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
figure(1);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r')
hold off;
figure(2);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r',logspace(-2,7,300))
hold off;

stop matlab line plots from overlapping

I plot many lines on top of one another, using plot and hold on
however i want one of the lines to be shifted a bit if it falls on another line.
for example in the following case:
plot(1:100); hold on; plot(-100:100,abs(-100:100))
i want it to be clear that there are 2 plots here
i tried to simply increase x values for different plots but this skews the data too much
for z=1:numberofplots
plot((1:size(locations,2))+0.1*z,locations(z,:)','color', altclrz(z,:));
end
You can differentiate the curves in several ways:
-1- Skewing the data
As you said, you could shift the data a bit. I would suggest fixing your axis and then calculating how many units in linewidth is so you get a very tight fit, like this:
lineWidth = 5;
figure(33);
clf;
subplot(1,2,1);
h = plot(myData, 'linewidth', lineWidth);
xlim([1,5]);
ylim([1,5]);
title('Original');
myData = meshgrid(1:5)';
myLimDiff = diff(ylim);
set(gca,'units', 'pixels');
myPos = get(gca, 'position')
myWidthHeight= myPos(3:4)
PixelsPerUnit =myWidthHeight(2)./ myLimDiff;
myDataSkewed = myData + meshgrid(-2:2)*1/PixelsPerUnit(1)*lineWidth;
subplot(1,2,2);
plot(myDataSkewed, 'linewidth', lineWidth);
xlim([1,5]);
ylim([1,5]);
title('Skewed');
Result:
-2- Using solid lines and Dashes
As someone else noted in the comments, you could you a dashed line over a solid line, or some combination of styles.
-3- Using different line thickness
Use different line widths with the thickest on the bottom:
figure(54);
clf
hold all
for ind = 10:-3:1
plot(1:5, 'linewidth', ind);
end
-4- Use separate plots for each line with a twist
Another way to call out each line is to plot each line in a subplot but to plot all the data in gray first. This way you can see where all the lines are with each particular line called out:
figure(55);
clf
data = rand(3);
for ind = 1:3
subplot(1,3,ind);
plot(data, 'linewidth', 4, 'color', [1 1 1]*.75);
hold on
plot(data(:,ind), 'linewidth', 2);
end