how can I get index from ginput?

조회 수: 6 (최근 30일)
Saad Alqahtani
Saad Alqahtani 2020년 9월 2일
편집: Binu 2023년 5월 14일
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!

답변 (1개)

Image Analyst
Image Analyst 2020년 9월 3일
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
Binu
Binu 2023년 5월 14일
편집: Binu 2023년 5월 14일
Hi, I found this today and it helped to solve my issue. Thank a lot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by