How to extract data points from a plot

236 ビュー (過去 30 日間)
Rose Sohyun Ahn
Rose Sohyun Ahn 2022 年 2 月 2 日
編集済み: Dave B 2022 年 2 月 3 日
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
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.
  2 件のコメント
Dave B
Dave B 2022 年 2 月 3 日
編集済み: Dave B 2022 年 2 月 3 日
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

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by