Closest index in the annotation of a signal

1 visualización (últimos 30 días)
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
Respondida: Ravi el 23 de En. de 2024
Hello everyone!I have a problem with the annotation of this wave. I need to annotate the points in red , which correspond to the index locs, but only the previous one and the after one with respect the point in black which correspond to the index locs_zero. In this way I don’t annotate the another points in red. Locs and locs_zero have two different dimensions.
  5 comentarios
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
i want to obtain something like this using the onset

Iniciar sesión para comentar.

Respuestas (1)

Ravi
Ravi el 23 de En. de 2024
Hi Lucrezia Mattia,
The key point here is to find the adjacent elements of the indices marked with black coloured circles. Let us define a function “findAdjacents” that takes input the index from “locs” array, and the locs_zero array. This function finds the indices of the left and right red circles to the index passed as an argument. Using this function, we can obtain the indices of all those red circles that we want to annotate.
Here is the code for the “findAdjacents” function.
function result = findAdjacents(index, target)
left = -1;
right = -1;
% Loop over the target array
for i=1:length(target)
% Find the value stored at current index i
redindex = target(i);
% If the redindex value is less than index, then it can be
% one of the red circles that is present to the right of desired
% black circle. The right red circle will be the next index of
% the last left circle to the desired black colour
if redindex < index
left = redindex;
if i + 1 <= length(target)
right = target(i + 1);
end
end
end
if left ~= -1
result = [left];
end
if right ~= -1
result = [result right];
end
end
Now, since I do not have the “signal” data that you have loaded, I have taken the following data.
square = @(x) x.*x;
x = -4:4;
y = square(x);
In this example, I will consider black markers are at positions 3 and 7, the remaining all are plotted with red. Now, ideally “findAdjacents” function should return the positions, 2, 4, 6, and 8.
blackcircles = [3, 7];
redcircles = [1, 2, 4, 5, 6, 8, 9];
marked = [];
for bidx = 1:length(blackcircles)
blackindex = blackcircles(bidx);
result = findAdjacents(blackindex, redcircles);
left = result(1);
right = result(2);
marked = [marked left right];
end
disp(marked);
2 4 6 8
% code related to plotting red and black circles is not added here as it
% was already present in the question
The next objective is to annotate these points. To annotate the points in a plot, we can use the “text” function.
for idx = 1:length(marked)
index = marked(idx);
text(x(index), y(index), "point" + num2str(idx));
end
To know more about the “text” function, please refer to the following link.
I hope this answers your question.

Categorías

Más información sobre Graphics Objects en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by