datacursormode & slope calculation
조회 수: 1 (최근 30일)
이전 댓글 표시
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:
- Select two points showing the black dot for both points
- Upon the second left-clicking, a line is drawn connecting the two points and a text box shows the calculated slope
- If I move either black dot, the text box is updated with a new slope
- I can have as many pairs as I want, many lines between a part of black dots
- 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
Steven Lord
2015년 9월 8일
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.
답변 (1개)
Image Analyst
2015년 9월 8일
Have you considered using imline()? I've attached an example using multiple imlines. Feel free to adapt it.
댓글 수: 2
Image Analyst
2015년 9월 9일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!