# Is there an interactive slope calculator between two moveable markers in a figure?

3 views (last 30 days)
Fredrik on 28 Feb 2014
Commented: Fredrik on 3 Mar 2014
Hi, I was wondering if anyone has created a way of interactively showing the slope between 2 points that one can move around in a figure. Lets say I plot something which is close to linear. Then I want to be able to set two markers, maybe one in the end and one in the beginning which can give me the slope between these two points. Then I want to be able to move these two points around to see the slope at different positions in the figure. This feature exists in many measurement programs and I would be suprised if matlab doesnt have this interactive feature. Any ideas?
Thanks, Fredrik Lindelöw

Fredrik on 3 Mar 2014
Since I haven't received any answers, i did an attempt to create a similar function my self.
% Calculates the slope and offset between two interactively chosen points % in a plot. Then plots the points plus curve and writes out slope and offset in the title of figure. % Can be used to calculate resistance in I-V curves for instance. % Select two points, first point to the left of second point.
function SlopeCalc
X=ginput;
k=(X(2,2)-X(1,2))/(X(2,1)-X(1,1));
m=X(1,2)-X(1,1)*k;
title({'Slope: ',num2str(k),' M value: ',num2str(m)})
plot([X(1,1),X(2,1)],[X(1,1),X(2,1)].*k+m,'ko-')
end
Fredrik on 3 Mar 2014
Using imline made it even better, though slower if many data sets are plotted in same image.
function SlopeCalc2
z=imline
addNewPositionCallback(z,@(z) title(['Slope: ',num2str((z(4)-z(3))/(z(2)-z(1))) ,' M value: ',num2str( z(3)-z(1)*((z(4)-z(3))/(z(2)-z(1))) ) ]));
end