how can I get index from ginput?

16 vues (au cours des 30 derniers jours)
Saad Alqahtani
Saad Alqahtani le 2 Sep 2020
Modifié(e) : Binu le 14 Mai 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!

Réponses (1)

Image Analyst
Image Analyst le 3 Sep 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 commentaire
Binu
Binu le 14 Mai 2023
Modifié(e) : Binu le 14 Mai 2023
Hi, I found this today and it helped to solve my issue. Thank a lot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by