How to extract data points from a plot
140 次查看(过去 30 天)
显示 更早的评论
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 个)
Dave B
2022-2-2
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 个评论
Ali
2023-1-20
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
2023-2-3
@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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!