How to get data points from graph

hi,
I have graph, i need to prepare a table of the data for x and y terms. How should i get the data points from the enclosed graph, please let me know.
Regards,
Syed Mohiuddin

2 Comments

It's not clear to me what you want to do.
What does 'terms' mean here?
Do you want to x and y coordinate values of the points used to plot that graph from the figure?
i want to have x and y coordinates values from the graph
Regards,
Syed Mohiuddin

Sign in to comment.

 Accepted Answer

Getting information from .fig files has changed over the years. This approach works with the most recent releases.
Use the findobj function with the axes handle to return specifc ‘Line’ objects (since there may be more than one), then index into them, for example:
Lines = findobj(Ax, 'Type','line')
X{1} = Lines(1).XData;
Y{1} = Lines(1).YData;
If there are more than one, this can be done in a loop.
There is only one here, so just do this —
openfig('data points.fig');
% Ax = hf.CurrentAxes; % One Option
Ax = gca; % Another Option
Lines = findobj(Ax, 'Type','line'); % Return Handles To All 'Line' Objects
x = Lines.XData % Independent Variable
x = 1×50
-1.0000 -0.9592 -0.9184 -0.8776 -0.8367 -0.7959 -0.7551 -0.7143 -0.6735 -0.6327 -0.5918 -0.5510 -0.5102 -0.4694 -0.4286 -0.3878 -0.3469 -0.3061 -0.2653 -0.2245 -0.1837 -0.1429 -0.1020 -0.0612 -0.0204 0.0204 0.0612 0.1020 0.1429 0.1837
y = Lines.YData % Dependent Variable
y = 1×50
0 0.0072 0.0143 0.0211 0.0278 0.0343 0.0407 0.0468 0.0528 0.0587 0.0643 0.0698 0.0752 0.0803 0.0852 0.0900 0.0946 0.0989 0.1030 0.1069 0.1106 0.1140 0.1171 0.1200 0.1225 0.1247 0.1266 0.1281 0.1293 0.1300
figure
plot(x, y, '-r', 'LineWidth',2)
grid
xlabel('x')
ylabel('y')
title('Recovered Data')
axis('equal') % Optional
.

11 Comments

Thank you for your answer, great. The data points that we get in Matlab is like column 1 to column13, column 14 to column 26 and so on. Is there any way that we express the output as vertically in a single column we get x values and similarly in a single column we get y values. However we can copy and paste the values in excel, but i want to explore the option of getting the solution in the form of table as shown
Thank you
Regards,
Syed Mohiuddin
As always, my pleasure!
Creating a table from those values is straightforward —
openfig('data points.fig');
set(gcf, 'Visible','off')
Ax = gca; % Another Option
Lines = findobj(Ax, 'Type','line'); % Return Handles To All 'Line' Objects
x = Lines.XData(:); % Independent Variable (Force Column Vector With '(:)')
y = Lines.YData(:); % Dependent Variable (Force Column Vector With '(:)')
Figure_Table = table(x,y, 'VariableNames',{'X values','Y values'})
Figure_Table = 50×2 table
X values Y values ________ _________ -1 0 -0.95918 0.0072262 -0.91837 0.014267 -0.87755 0.021128 -0.83673 0.027812 -0.79592 0.034324 -0.7551 0.040665 -0.71429 0.046838 -0.67347 0.052841 -0.63265 0.058677 -0.59184 0.064342 -0.55102 0.069834 -0.5102 0.075151 -0.46939 0.080288 -0.42857 0.085238 -0.38776 0.089997
If you want to create an Excel file from it, use the writetable function and set the file extension as .xlsx
writetable(Figure_Table, 'Data_From_Figure.xlsx')
New_Table = readtable('Data_From_Figure.xlsx', 'VariableNamingRule','preserve')
New_Table = 50×2 table
X values Y values ________ _________ -1 0 -0.95918 0.0072262 -0.91837 0.014267 -0.87755 0.021128 -0.83673 0.027812 -0.79592 0.034324 -0.7551 0.040665 -0.71429 0.046838 -0.67347 0.052841 -0.63265 0.058677 -0.59184 0.064342 -0.55102 0.069834 -0.5102 0.075151 -0.46939 0.080288 -0.42857 0.085238 -0.38776 0.089997
.
Excellent, great, i got what i wanted. Thanks a lot, i am very happy to get this table format
why is that i am getting an error
Error in table (line 256)
t.varDim = matlab.internal.table.tableVarNamesDim(numVars,varnames); % error if invalid, duplicate, or empty
Error in extractnewnew (line 10)
Figure_Table = table(x,y, 'VariableNames',{'X values','Y values'})
As always, my pleasure!
My code works with the current version of MATLAB. I am not certain where that error is being thrown.
I do not know what MATLAB version you are using, however in earlier versions (I beleive before R2020b) in readtable, 'VariableNamingRule','preserve' was 'PreserveVariableNames',true.
Also in previous versions, spaces and other special characters in variable names were not permitted. The variable names had to be valid MATLAB variable names, so you would use:
Figure_Table = table(x,y, 'VariableNames',{'X_values','Y_values'})
instead.
If this does not solve that problem, please post a follow-up comment and copy and paste all the red text from your Command Window to it so I can figure out what the problem is. Also, please post the MATLAB version you are using.
.
Great, i got the table, thanks a lot. I am using R2016b version and your code worked. Is it possible i can extract a definite data as shown in the table
or for the matter, for any type of specified values of x, can i get the values of y
Regards,
Syed Mohiuddin
As always, my pleasure!
Only two of those numbers actually exist in ‘X values’ so using ismember or ismembertol will not return exact matches. The solution to getting all of them is to use interp1 instead.
I tried both here, to be certain —
openfig('data points.fig');
set(gcf, 'Visible','off')
Ax = gca; % Another Option
Lines = findobj(Ax, 'Type','line'); % Return Handles To All 'Line' Objects
x = Lines.XData(:); % Independent Variable (Force Column Vector With '(:)')
y = Lines.YData(:); % Dependent Variable (Force Column Vector With '(:)')
Figure_Table = table(x,y, 'VariableNames',{'X values','Y values'})
Figure_Table = 50×2 table
X values Y values ________ _________ -1 0 -0.95918 0.0072262 -0.91837 0.014267 -0.87755 0.021128 -0.83673 0.027812 -0.79592 0.034324 -0.7551 0.040665 -0.71429 0.046838 -0.67347 0.052841 -0.63265 0.058677 -0.59184 0.064342 -0.55102 0.069834 -0.5102 0.075151 -0.46939 0.080288 -0.42857 0.085238 -0.38776 0.089997
Extract_Table = Figure_Table(ismembertol(Figure_Table.('X values'), [1; 0.4; 0; -0.4; -1], 0.02),:)
Extract_Table = 4×2 table
X values Y values ________ ________ -1 0 -0.38776 0.089997 0.38776 0.12622 1 0
Interpolate_Table = table('Size',[5 2], 'VariableTypes',{'double','double'}, 'VariableNames',{'X values','Y values'});
Interpolate_Table.('X values') = [1; 0.4; 0; -0.4; -1];
Interpolate_Table.('Y values') = interp1(Figure_Table.('X values'), Figure_Table.('Y values'), [1; 0.4; 0; -0.4; -1])
Interpolate_Table = 5×2 table
X values Y values ________ ________ 1 0 0.4 0.1255 0 0.12364 -0.4 0.08857 -1 0
You can get other interpolated values as well using interp1, however you would need to expand the initial table definition (the first ‘Interpolate_Table’ assignment here) or do the interpolation outside of the table using the table variables (see Access Data in Tables for those details) and then create a table array to store them.
.
Thanks for the solution, but I am getting the error as
"Error in extractnewnew (line 12)
Interpolate_Table = table('Size',[5 2], 'VariableTypes',{'double','double'}, 'VariableNames',{'X_values','Y_values'});
Caused by:
You may have intended to create a table with one row from one or more variables that are character vectors. Consider using
strings or cell arrays of character vectors rather than character arrays. Alternatively, create a cell array with one row, and
convert that to a table using CELL2TABLE."
Please help me to remove this error
Regards,
Syed Mohiuddin
That obviously does not work in R2016b.
It will likely be easier to just do the interpolation outside the table (using the extracted variables) and then create a new table from the interpolated results.
Try this —
openfig('data points.fig');
set(gcf, 'Visible','off')
Ax = gca; % Another Option
Lines = findobj(Ax, 'Type','line'); % Return Handles To All 'Line' Objects
x = Lines.XData(:); % Independent Variable (Force Column Vector With '(:)')
y = Lines.YData(:); % Dependent Variable (Force Column Vector With '(:)')
Figure_Table = table(x,y, 'VariableNames',{'X values','Y values'})
Figure_Table = 50×2 table
X values Y values ________ _________ -1 0 -0.95918 0.0072262 -0.91837 0.014267 -0.87755 0.021128 -0.83673 0.027812 -0.79592 0.034324 -0.7551 0.040665 -0.71429 0.046838 -0.67347 0.052841 -0.63265 0.058677 -0.59184 0.064342 -0.55102 0.069834 -0.5102 0.075151 -0.46939 0.080288 -0.42857 0.085238 -0.38776 0.089997
xv = Figure_Table{:,1}; % Extract 'X values'
yv = Figure_Table{:,2}; % Extract 'Y values'
xq = [1; 0.4; 0; -0.4; -1]; % Define Desired X-Values To Interpolate
yq = interp1(xv, yv, xq); % Interpolate
Interpolate_Table = table(xq, yq, 'VariableNames',{'X values','Y values'}) % Create New Table With Interpolated VAlues
Interpolate_Table = 5×2 table
X values Y values ________ ________ 1 0 0.4 0.1255 0 0.12364 -0.4 0.08857 -1 0
That should work for any values of ‘xq’ you may want to use, providing they are all within the current minimum and maximum boundaries of ‘X values’. If they are not, that means that you are extrapolating them and that requires a slightly different approach. If you want to do that, see the interp1 documentation section on extrapolation.
.
Thank you thank you thank you, great, this was exactly what i wanted.
As always, my pleasure!

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 1 Dec 2023
Edited: Voss on 1 Dec 2023
hello
the main code is very simple (use the attached function - credits to his creator !)
data = extract_data_from_figures('data points.fig');
VN = data.names; % variable names
data = data.Y;
x = data(:,1);
y = data(:,2);
figure(1)
plot(x,y);

3 Comments

if i run the program, it gives error saying ' undefined function or variable 'extract_data_from_figures' . How to remove this error
you probably did not download the function with the link I mentionned above
for your convenience I attach here to this post

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!