Filter löschen
Filter löschen

how can I get index from ginput?

20 Ansichten (letzte 30 Tage)
Saad Alqahtani
Saad Alqahtani am 2 Sep. 2020
Bearbeitet: Binu am 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!

Antworten (1)

Image Analyst
Image Analyst am 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 Kommentar
Binu
Binu am 14 Mai 2023
Bearbeitet: Binu am 14 Mai 2023
Hi, I found this today and it helped to solve my issue. Thank a lot.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by