Hi,
In the attached array, I need to color the region based on whenever A(:,2) is greater than 5. I tried yregion,seems this will work only in 2023.

2 comentarios

Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2023
Which version of MATLAB are you using?
If you want an output similar to what yregion provies, what is the upper limit for the region?
If it is not similar to that, what is the expected output? A visual example would be helpful.
Dyuman Joshi
Dyuman Joshi el 2 de Nov. de 2023
You forgot to provide the end values -
Red color from 5 to what? Inf?
Similarly, green color from what to 5? -Inf?
And I suppose the x-limits should be the range of the x values i.e. A(:,1)?

Iniciar sesión para comentar.

 Respuesta aceptada

Try this —
LD = load('matlab.mat');
A = LD.A
A = 12493×2
-5.3531 1.3833 -5.1375 1.5149 -5.1375 1.5471 -5.1375 1.6226 -5.1375 1.6774 -5.1375 1.3851 -5.1375 1.5581 -5.1375 1.5314 -4.9938 1.7729 -4.9938 1.5963
thrshld = 5;
Lv = A(:,2) > thrshld;
idx1 = find(Lv);
idx2 = idx1(diff([0; idx1]) == 1);
% [x1,x2] = bounds(idx2)
idx2 = min(idx2) : max(idx2);
figure
plot(A(:,1), A(:,2))
hold on
patch([A(idx2,1); flip(A(idx2,1))], [thrshld+zeros(size(A(idx2,2))); flip(A(idx2,2))], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
patch([A(idx2,1); flip(A(idx2,1))], [min(ylim)+zeros(size(A(idx2,2))); thrshld+zeros(size(A(idx2,2)))], 'g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
grid
.

3 comentarios

As always, my pleasure!
I was having problems getting the green area to work correctly.
Try this —
LD = load('matlab.mat');
A = LD.A
A = 12493×2
-5.3531 1.3833 -5.1375 1.5149 -5.1375 1.5471 -5.1375 1.6226 -5.1375 1.6774 -5.1375 1.3851 -5.1375 1.5581 -5.1375 1.5314 -4.9938 1.7729 -4.9938 1.5963
thrshld = 5;
Lv1 = A(:,2) > thrshld;
Lv2 = A(:,2) <= thrshld;
figure
plot(A(:,1), A(:,2), '.-')
hold on
patch([A(Lv1,1); flip(A(Lv1,1))], [thrshld+zeros(size(A(Lv1,2))); flip(A(Lv1,2))], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
patch([A(:,1); flip(A(:,1))], [min(ylim)*ones(size(A(:,2))); min([thrshld+zeros(size(A(:,2))) flip(A(:,2))],[],2)], 'g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
grid
.
Perfect! Thanks again
Star Strider
Star Strider el 2 de Nov. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Labels and Annotations en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Nov. de 2023

Comentada:

el 2 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by