how can I get index from ginput?

18 visualizaciones (últimos 30 días)
Saad Alqahtani
Saad Alqahtani el 2 de Sept. de 2020
Editada: Binu el 14 de Mayo de 2023
Hello,
I need a help on how to get index from ginput on a plot:
I have x axis = time(timef), yaxis = force(datan):
I've tried this code bellow but it doesn't work:
figure, plot( timef, datan, 'LineWidth',1.5, 'Color', '[0.301,0.745,0.933]' );
title('Calibrated Force')
xlabel('Time (s)')
% start/end time range
for i = 1:1
[strt(i),~] = ginput(1);
[fin(i),~] = ginput(1);
calcStart = find(timef == round(strt(i)));
calcEnd = find(timef == round(fin(i)));
Thanks!

Respuestas (1)

Image Analyst
Image Analyst el 3 de Sept. de 2020
ginput(1) asks the user to click on one point and it returns the x and y value of that point. There is no index since it's just one points. Then you take the x value and stick in into strt(i). Then you repeat that and put the next x value where the user clicks and put it into fin(i).
What I think you really want to do is to find out what index of your array is closest to the x value of where they clicked. Untested code:
for k = 1:1
uiwait(helpdlg('Please click on the starting and ending points.'));
[x, y] = ginput(2);
distances1 = abs(timef - x(1));
[minDistance, indexOfMin1] = min(distances1);
distances2 = abs(timef - x(2));
[minDistance, indexOfMin2] = min(distances2);
indexStart(k) = indexOfMin1; % Save index of starting time.
indexEnd(k) = indexOfMin2; % Save index of ending time.
timeStart(k) = timef(indexOfMin1); % Save starting time.
timeEnd(k) = timef(indexOfMin2); % Save ending time.
end
  1 comentario
Binu
Binu el 14 de Mayo de 2023
Editada: Binu el 14 de Mayo de 2023
Hi, I found this today and it helped to solve my issue. Thank a lot.

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by