How do I add horizontal lines and text to a wcoherence plot?

4 views (last 30 days)
Hi everyone,
I'm trying to draw a horizontal line and add some text to a wcoherence plot (because I'd rather do it in Matlab than in Illustrator afterwards). Using the first example in https://www.mathworks.com/help/wavelet/ref/wcoherence.html
figure
rng default;
t = 0:0.001:2;
x = cos(2*pi*10*t).*(t>=0.5 & t<1.1)+ ...
cos(2*pi*50*t).*(t>= 0.2 & t< 1.4)+0.25*randn(size(t));
y = sin(2*pi*10*t).*(t>=0.6 & t<1.2)+...
sin(2*pi*50*t).*(t>= 0.4 & t<1.6)+ 0.35*randn(size(t));
wcoherence(x,y,seconds(0.001));
How would I add a horizontal white line at 0.2 seconds and the text "0.2 seconds" right above it? I know it's possible because the following line in wcoherence (723) plots the cone of influence:
plot(ax,t,log2(coi),'w--','linewidth',2)
I've tried pasting the following code into the wcoherence function, right after the cone of influence plot ^, but no matter what I do, the line and text don't show up.
linex = [min(t) max(t)];
liney = [0.2 0.2];
plot (AX, linex, log2(liney), 'w-', 'linewidth', 1);
text (min(t)+0.1, log2(0.2+0.05), '0.2 seconds')
I suspect it's something with the 'layer' of the plot or that I'm pasting it in the wrong part of wcoherence or that I'm misunderstanding the conversions between frequencies and periods and log scaling, but I'm stumped.
Thanks so much for your help!

Accepted Answer

Sakshay
Sakshay on 30 Nov 2022
Hello Bryce,
As per my understanding, you are trying to plot a horizontal line, on an already generated wcoherence plot.
To retain the current plot in MATLAB, we have to use the "hold on" function. "hold on" retains plots in the current axes so that new plots added to the axes do not delete existing plots. For your case the script would look like:
figure;
rng default;
t = 0:0.001:2;
x = cos(2*pi*10*t).*(t>=0.5 & t<1.1)+ ...
cos(2*pi*50*t).*(t>= 0.2 & t< 1.4)+0.25*randn(size(t));
y = sin(2*pi*10*t).*(t>=0.6 & t<1.2)+...
sin(2*pi*50*t).*(t>= 0.4 & t<1.6)+ 0.35*randn(size(t));
wcoherence(x,y,seconds(0.001));
% Hold the previous plot to generate the next plots over it
hold on
linex = [min(t) max(t)];
liney = [0.2 0.2];
plot (linex, log2(liney), 'w-', 'linewidth', 1);
text (min(t)+0.1, log2(0.2+0.05), '0.2 seconds');
You can refer to the following documentation on "hold" for more information:
  1 Comment
Bryce Mitsunaga
Bryce Mitsunaga on 27 Dec 2022
Hi Sakshay,
Sorry for the super slow reply, but ...yup, that was it, thank you so much! I thought the
hold(AX,'on');
around line 720 would do it, but it makes sense that you'd need a separate hold for subsequent plots at the end / over it.

Sign in to comment.

More Answers (0)

Categories

Find more on Wavelet Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!