229 views (last 30 days)
Show older comments
Rose Sohyun Ahn on 2 Feb 2022
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
Show -1 older commentsHide -1 older comments
Arif Hoq on 2 Feb 2022
Direct link to this comment
https://uk.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
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"
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.
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Rose Sohyun Ahn on 3 Feb 2022
Direct link to this comment
https://uk.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://uk.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');
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://uk.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://uk.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.
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)
- 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