how I use ginput and get a y and z?

1 次查看(过去 30 天)
sama
sama 2015-4-21
评论: Adam 2015-4-21
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.

回答(1 个)

Adam
Adam 2015-4-21
编辑:Adam 2015-4-21
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 个评论
sama
sama 2015-4-21
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 2015-4-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by