calculating and displaying the slope of a line

2 visualizzazioni (ultimi 30 giorni)
Eric Hong
Eric Hong il 2 Set 2015
Modificato: Stephen23 il 8 Set 2015
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

Risposte (2)

Purushottama Rao
Purushottama Rao il 3 Set 2015
Modificato: Stephen23 il 8 Set 2015
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 Commento
Eric Hong
Eric Hong il 7 Set 2015
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

Accedi per commentare.


Image Analyst
Image Analyst il 3 Set 2015
Use sprintf() to create your string, then use text() to place it wherever you want.
  2 Commenti
Image Analyst
Image Analyst il 7 Set 2015
"It deletes all other text boxes" <-- then why do you call delete at all?
Eric Hong
Eric Hong il 8 Set 2015
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

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by