Subjects > MATLAB > Practice Test > Data Extractor

MATLAB

Contents

% Author: J. Kadoko
% This script helps you manually extract data points on a plot that is saved as an 
% image file eg jpg, png, gif
% It uses least squares to obtain the actual values from pixels values

% Possible Use: you need to generate a MATLAB model but one piece of your
% data set is in the form of graphs. Assuming your model only takes
% numerical input, you need a way to read the graph. That is where this
% script comes in.

% Posible future development: Use image processing to extract values.

% inputs
%     graph plot as an image
%     the maximum points in the plot (these are reference points)

% outputs
%     points from the graph in desired units

Extracting 3 points in pixels from the image

clf;clear
addpath('C:\Users\User\Documents\MATLAB','C:\Users\User\Documents\MATLAB\Plot Extractor')
fname='spectral_Response.PNG';
imshow(fname);

% Input Variables [real-world units]:
h0=300; % The origin
v0=0; % The origin
hMax=1300; % The maximum point on horizontal axis
vMax=0.6; % The maximum point on vertical axis
% Input Variables [pixels]:

[x,y]=ginput(3);
x0=[x(1) y(1)]; % The origin
x1=[x(2) y(2)]; % The maximum point on horizontal axis
y1=[x(3) y(3)]; % The maximum point on vertical axis

Transforming the horizontal axis

% Matrices for regression T=bT+cT*u
u=[x0(1);x1(1)];
H=[h0;hMax];
bc=[ones(size(u)) u]\H;
hOffset=bc(1); %  offset
hScale=bc(2); %  scale factor

Transforming the vertical Axis

these points correspond to the x0,x1 and y1

% Matrices for regression P=bP+cP*v
v=[x0(2);y1(2)];
V=[v0;vMax];
bc=[ones(size(v)) v]\V;
vOffset=bc(1); %  offset
vScale=bc(2); %  scale factor

Extracting Points from the graph

for iter=1:10

xi=ginput(1) ; % getting input from the image using a cursor
x_Pix(iter,1)=xi(1);
y_Pix(iter,1)=xi(2);

y_Ext(iter,1)=vOffset+vScale*xi(2);
x_Ext(iter,1)=hOffset+hScale*xi(1);
end

Plotting the extracted points

subplot(1,2,1)
imshow(fname);hold on;
plot(x_Pix,y_Pix,'x','LineWidth',3);
hold off;

subplot(1,2,2)
plot(x_Ext,y_Ext,'--x','LineWidth',3)
title 'The plot of extracted x and y points'
xlabel 'x values'
ylabel 'y values'
grid on
Scroll to Top