How to extract data points from a plot (2024)

227 views (last 30 days)

Show older comments

Rose Sohyun Ahn on 2 Feb 2022

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot

Commented: Dave B on 3 Feb 2023

  • hw1_kinetics.m

Hello,

I'm doing homework through matlab and I'm trying to find certain points that will fit my answer through matlab plotting. Is there a way I can click on the graph and extract x and y data points arbitrarily?

Thank you !

I have attached my very simple code for clarification.

1 Comment

Show -1 older commentsHide -1 older comments

Arif Hoq on 2 Feb 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1966535

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1966535

It can be helpful

https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures

Sign in to comment.

Sign in to answer this question.

Answers (1)

Dave B on 2 Feb 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#answer_887330

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#answer_887330

To interactively select points to export data to the workspace, there are a couple of options - using datatips is great for single points, data brushing works better for multiple points:

One point

For most MATLAB plots, you'll see a little box (called a datatip) when you hover over points. If you click while a point is highlighted, the box stays put when you move your mouse away. If you then right click you'll see an option to "Export Cursor Data to Workspace"

How to extract data points from a plot (4)

How to extract data points from a plot (5)

How to extract data points from a plot (6)

Multiple Points

When you hover over the axes a little toolbar shows up in the upper right corner. One of the options in that toolbar looks like a little paintbrush. Turn this on to activate "DataBrushing" mode. You can click and drag to select some points you'd like to export. When you've selected the points you want, disable brushing (by clicking the paintbrush again) and right click somewhere on the axes. You'll see an option to Export Brushed, which will export a variable containing the data points.

How to extract data points from a plot (7)

How to extract data points from a plot (8)

How to extract data points from a plot (9)

4 Comments

Show 2 older commentsHide 2 older comments

Rose Sohyun Ahn on 3 Feb 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1967665

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1967665

how about data points between 3 and 4 let's say?

Dave B on 3 Feb 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1968485

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_1968485

Edited: Dave B on 3 Feb 2022

Do you mean: how would you extract the set of data points programatically (i.e. not by clicking) from a line chart (i.e. made with the plot function) where x is between 3 and 4? You could query the XData and YData of the Line object like so:

x=linspace(0,5,50);

y=rand(size(x));

h=plot(x,y,'-o');

How to extract data points from a plot (12)

h.XData(h.XData >= 3 & h.YData <=4)

ans = 1×20

3.0612 3.1633 3.2653 3.3673 3.4694 3.5714 3.6735 3.7755 3.8776 3.9796 4.0816 4.1837 4.2857 4.3878 4.4898 4.5918 4.6939 4.7959 4.8980 5.0000

h.YData(h.XData >= 3 & h.YData <=4)

ans = 1×20

0.9329 0.7297 0.3868 0.1009 0.4098 0.5118 0.6134 0.7609 0.7607 0.7859 0.2555 0.4924 0.2036 0.9361 0.7540 0.6673 0.8542 0.7673 0.0565 0.6882

Ali on 20 Jan 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_2574140

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_2574140

It works, thank you!

In the first method , can we get the x and y coordinates seperately instead of getting them in a [x,y] format?

further, there is also a column called "data index". is that related to the order of the data points?

does it indicate which data was plotted before the other one?

Dave B on 3 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_2596376

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1641350-how-to-extract-data-points-from-a-plot#comment_2596376

@Ali -

I don't think there's a setting that will return the coordinates in separate variables, although it's pretty easy to move between vectors and variables.

x=cursor_info.Position(1);

y=cursor_info.Position(2);

If you had multiple datatips and wanted to grab all of them at once, you might do something like:

xy=vertcat(cursor_info.Position)

x=xy(:,1);y=xy(:,2);

Note that some plots (like surf or contour) might have a third dimension.

I think the DataIndex is the index into XData/YData properties, which corresponds to the order that you passed them into plot, but I'm not sure if that's your question. This might be a little more complicated if you don't have the "Snap to Nearest Data Vertex" option selected in Selection Style.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D Plots

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

  • plot
  • data acquisition

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to extract data points from a plot (15)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to extract data points from a plot (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5889

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.