datacursormode & slope calculation

1 view (last 30 days)
Eric Hong
Eric Hong on 8 Sep 2015
Commented: Image Analyst on 9 Sep 2015
I'm trying to calculate the slope between two points. I've tried some other methods such as imline, but none of them allows the snapping to a curve. I found out this morning that I can possibly use the data tips in the datacursormode.
So, what I want to do is:
  1. Select two points showing the black dot for both points
  2. Upon the second left-clicking, a line is drawn connecting the two points and a text box shows the calculated slope
  3. If I move either black dot, the text box is updated with a new slope
  4. I can have as many pairs as I want, many lines between a part of black dots
  5. If either dot is deleted, the associated text box disappears along with it
I've tried things like
waitforbuttonpress;
point1 = getCursorInfo(data_cursor_mode_obj);
waitforbuttonpress;
point2 = getCursorInfo(data_cursor_mode_obj);
slope = abs((y2-y1)/(x2-x1));
text((x1+x2)/2,(y1+y2)/2,[' \leftarrow' num2str(slope) ])
This of course doesn't update the text. In fact, for some reason, Matlab complains and I don't know how to fix it.
Another thing I tried was using UpdateFcn, but a problem with it is that I can only pass one point data by doing event_obj.Position.
Your help will be greatly appreciated.
Thanks,
Eric
  2 Comments
Steven Lord
Steven Lord on 8 Sep 2015
Define "Matlab[sic] complains" -- does it issue a warning? Does it throw an error? In either of those cases, what is the FULL text of the error message you receive?
You probably want to use a WHILE loop with a PAUSE inside to wait until getCursorInfo returns a struct array with at least two elements, to ensure you have at least two points selected.
Eric Hong
Eric Hong on 8 Sep 2015
Field reference for multiple structure elements that is followed by more reference blocks is an error.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 Sep 2015
Have you considered using imline()? I've attached an example using multiple imlines. Feel free to adapt it.
  2 Comments
Eric Hong
Eric Hong on 8 Sep 2015
yes. I have and that's currently what I'm using. However, I really want the snapping to a curve. imline doesn't do that, as far as I understand. Is that true?
Image Analyst
Image Analyst on 9 Sep 2015
No, you'd have to do that manually. There is no "snapping" of the imline() endpoints to other data that may happen to be living in your plot/graph.
I do something similar in one app. I have a curve (plot of a 1-D signal) and I have two scrollbars, one to move a vertical line that defines the left boundary, and one to move a vertical line that defines the right limit. I identify the Y value for those x limit locations and then draw a line from (x1,y1) to (x2,y2). Then I calculate the slope. Each time they move a scroll bar, it calls a function I wrote called ScrollBarMoved() which erases all the lines, then draws the two vertical lines and a slanted line between where the vertical lines cross the y curve.
Maybe that method would work for you.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!