How to select a number of points from a 2D matlab plot?

I have created a matlab profile plot of an image and I would like to know if there is a way that would enable the user to select points from the 2D plot so that he can later on draw a best fit line on those points.
Thank you in advance

 Réponse acceptée

Thorsten
Thorsten le 13 Fév 2013

0 votes

ginput does this job.

2 commentaires

UCL
UCL le 13 Fév 2013
Thanks for that. Would you know how to now plot these points with a line of best fit across them? I want a straight line of best fit so as to find its equation as well. I know polyfit might work, but not how.
Thank you
If you ask a new question, please do so by starting a new question so others will have the chance to answer, too.

Connectez-vous pour commenter.

Plus de réponses (1)

UCL, try this demo:
% Make image that they can click on.
plot(cos(1:30),'b-');
grid on;
uiwait(msgbox('Click 5 points'));
[x y] = ginput(5)
hold on;
plot(x, y, 'r+', 'LineWidth', 2);
% Do the least squares fit
coeffs = polyfit(x, y, 1);
% Now find estimated values at
% locations other than where they clicked.
xFitted = 1:30; % x values that we want the fit for.
yFitted = polyval(coeffs, xFitted);
plot(xFitted, yFitted, 'r-', 'LineWidth', 2);

4 commentaires

UCL
UCL le 14 Fév 2013
i tried this demo on my own profile plot but it only draws the line through the first 2-3 selected points and a bit more but then does not go through the other selected points which are on the opposite side of the plot.
UCL
UCL le 14 Fév 2013
i seem to have fixed it by increasing the xFitted=1:30 to 1:50 is that correct?
It worked fine for me. It puts "+" markers where you click, and then it fits a line through the range 1-30 and draws a line with no markers through that range. You can fit over whatever range you want. I just picked 1 to 30 arbitrarily.
UCL
UCL le 14 Fév 2013
thank you very much !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Exploration dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by