Closest index in the annotation of a signal
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Ravi
le 23 Jan 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);
% 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Graphics Objects dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!