how can I get index from ginput?

17 次查看(过去 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