calculating and displaying the slope of a line

조회 수: 10 (최근 30일)
Eric Hong
Eric Hong 2015년 9월 2일
편집: Stephen23 2015년 9월 8일
I create a figure with toggle button on its toolbar by uitoggletool. The callback function for it is shown below:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy=imline;
addNewPositionCallback(xy,@(xy)...
title(['\DeltaY/\DeltaX = ',num2str((xy(4)-xy(3))/(xy(2)-xy(1))),...
'[\DeltaX = ',num2str(xy(2)-xy(1)),...
',\DeltaY = ',num2str((xy(4)-xy(3))),']']));
end
As you can see, the example outputs the position data in the title of a figure using the 'title' command.
Is there a way to output this in a text box using the 'text' command?
I want to display the slope next to the line drawn.
Also, it will be great if the text box also gets deleted together with the associated line.
Please, help.
Thank you,
Eric

답변 (2개)

Purushottama Rao
Purushottama Rao 2015년 9월 3일
편집: Stephen23 2015년 9월 8일
You can try uicontrol. Change the position coordinates according to your requirement. The same thing can be expanded for delta x and delta y as well.
uicontrol('Style','text','String',num2str(num2str((xy(4)-xy(3))/(xy(2)-xy(1)))),'Position',[120 350 300 25],'Fontsize',18);
  댓글 수: 1
Eric Hong
Eric Hong 2015년 9월 7일
Thank you for your answer. The intention is to have a text box that shows the slope of a line for individual lines. I am struggling through my way here, but I've made some progress.
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy = imline;
addNewPositionCallback(xy,@(pos) disp_slope(pos));
end
function disp_slope(pos)
delete(findobj(gca,'Type','text'));
text((pos(1)+pos(2))/2,(pos(3)+pos(4))/2,['\DeltaY/\DeltaX = ',num2str((pos(4)-pos(3))/(pos(2)-pos(1))),...
' [\DeltaX = ',num2str(pos(2)-pos(1)),', \DeltaY = ',num2str((pos(4)-pos(3))),']']);
So, each toggle on the toggle button in the figure will throw in a draggable/resizable line and as I move the line the slope and some other information shows and updates, which looks pretty close to what I want. There are, however, two issues:
  1. It deletes all other text boxes except for the very last (current) text box of the line I'm moving around. I want to keep the last value remained and shown for all existing lines.
  2. If I delete a line by right-clicking on the line and choose 'the delete', it deletes the line, but not the text box and rightfully so. Now I have a text box that shows the slope of the line that exists no longer in the figure. I want the text box to disappear along with the line.
I'm having these problems and really not going anywhere because imline object behaves so much different than other typical objects and also the concept of the addNewPositionCallback is quite convoluted.
Please, somebody enlighten me on this.
Many thanks in advance,
Eric

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 9월 3일
Use sprintf() to create your string, then use text() to place it wherever you want.
  댓글 수: 2
Image Analyst
Image Analyst 2015년 9월 7일
"It deletes all other text boxes" <-- then why do you call delete at all?
Eric Hong
Eric Hong 2015년 9월 8일
It's just a mere attempt to have just one text box shown. Without it, as I drag a line around, millions of text boxes will stay in the figure.
If I can somehow delete only the text boxes associated with a particular line, that's what I want. I just don't know how. So, that I just decide to delete all for now since I'm using this plotting tool daily. It at least calculates the slope of a curve quickly and doesn't leave crazily overlapping text boxes.
I tried to use uicontrol and update the text, but what imline returns, xy, can't be a parent (Matlab says). I also tried to findobj a particular line and use that as a parent. Again, Matlab complains that it can't be used as a parent.
I'm lost without hope.
Thank you,
Eric

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by