how I use ginput and get a y and z?

2 views (last 30 days)
sama
sama on 21 Apr 2015
Commented: Adam on 21 Apr 2015
Hello. I need your help please. I am solving two equations based on time. imagine x and y are solved based on time. however I plot y vs x.I select 2 points by ginput and get x and y. do you have any idea how I can find the t related to x and y please? (both are calculating at same time. how ever time=t+delta at each points)
Thank you so much in advance.

Answers (1)

Adam
Adam on 21 Apr 2015
Edited: Adam on 21 Apr 2015
Here is an example of the kind of thing you want. I haven't checked the exact syntax in Matlab itself so there may be errors, but the idea should work.
t = 0:0.01:10;
x = sin( t );
y = cos(t);
figure; plot( x, y );
[x0, y0] = ginput( );
[~, idx] = min( abs( x - x0 ) );
t0 = t(idx);
  2 Comments
sama
sama on 21 Apr 2015
Thank you so much for prompt response. t0 gives always a same answer. Also I didn;t get the idea of [~,idx]=min(x-x0). Appreciate if you give me more information. thanks a gain
Adam
Adam on 21 Apr 2015
I just corrected the answer. Note the addition of the 'abs' inside the min function.
This line:
[~, idx] = min( abs( x - x0 ) );
is used to find the closest value of x to the x0 value picked using ginput. You could do the same with y if you want, the result should be the same if you click close enough to the line.
The second output argument from the min function is the index at which the minimum value is located. Here we don't care about the actual minimum value (the first argument) though if you want you can use this (and the y - y0 equivalent) as a measure of how close to the plot the ginput pick was.

Sign in to comment.

Categories

Find more on Data Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!