Originally MATLAB will not, this two days did a contest and a project related things, while Baidu side write, are tears, summed up the basis of the drawing function, can not be forgotten ... The code snippets are all written, not changed.
1. Reading the data to be processed from Excel
You can use the Xlsread function to read the functions you want to work with in Excel, and to see their usage through help xlsread
[Num,txt,raw]=xlsread (file)% reads the data from the first work page, NUM returns the number of data, TXT returns the text information in the table, the data that cannot be processed is returned in RAW
[Num,txt,raw]=xlsread (file , SHEET)% specifies a specific work page
[Num,txt,raw]=xlsread (file,sheet,range)% specifies a specific range of work pages, through such as ' c1:e4 ', the XLS suffix document is not supported in basic mode
[Num,txt,raw]=xlsread (File,sheet,range, ' basic ')% only supports xlsx documents
[Num,txt,raw]=xlsread (File,range)% reads the data from the first work page for the specified RANGE page and does not support XLS files in basic mode
Use a lot, and understand that some basic usage is enough for daily usage. As follows:
Matchleft=xlsread (' matchpoint.xlsx ', ' Sheet1 ', ' b1:c11 ');
If the data read has many columns, how to extract a specified column of data. As below, extract the first column of data
Matchleftx=matchleft (:, 1); % left location X coordinate
You can also specify which elements to access by ordinal and column numbers, such as accessing the data in column 3rd of row 6th as follows:
Matchleft (6,3);
2. For a given coordinate, draw it on the graph
Plot a point with the plot function, after you get an array of coordinates, you can draw it in the graph using the plot function, remembering that you don't need to use loops ... As follows
Plot (matchleftx,matchlefty, ' * ');% use ' * ' to identify points
The plot default is a solid blue line without a tag, so you can also use the plot function to concatenate sitting punctuation.
>> a=[1 2 3 4 5];
>> b=[1 2 3 4 5];
>> plot (A, B)
The results are as shown in the figure:
3. Calculate the linear slope between two points
For a given coordinate point, the Polyfit function calculates its y=kx+b-based slope and intercept, Polyfit can return a minimum of two values, the first value is the slope, the second value is the Intercept, or the two sets of coordinates just given
>> Polyfit (a,b,1)
warning:function * * *
ans =
1.0000 -0.0000
I gave a warning that it could be a data problem. Polyfit is used for polynomial curve fitting, and the mathematical basis is the least square method. The calling method is Polyfit (x,y,n), x is the horizontal axis, Y is the ordinate, N is the order to fit, not the higher the order, the better, to see the fitting situation depends. First-order linear fitting, second-line parabola fitting ...
There is also a polyval function, which can be calculated based on the slope of the specified point value such as:
Y0=polyval (p,x0)
P is the slope obtained by using Polyfit, x0 is the horizontal axis can find its corresponding ordinate value.
The Linspace function is an evenly calculated instruction in MATLAB using the following:
Linspace (X1,x2,n);
The vector used to generate n-point lines between x1,x2, where X1,x2,n is the starting value, the terminating value, the number of elements, and the default value of N is 100.
4. Draw the corresponding line for the given coordinates
According to the above, in fact the preparation is ready to complete, it is important to note that when determining linspace, because it is difficult to specify a reasonable range, we can specify the minimum and maximum values in the data by the Min and Max functions as the starting and ending values
P=polyfit (x,y,1);
X1=linspace (min (x), Max (x));
Y1=polyval (p,x1);
Plot (x, y, ' * ', x1,y1);
For spatial three-dimensional coordinate points, X, Y, Z is a set of coordinate points, the least squares for linear fitting There are many ways, here I introduce a relatively simple method, example data capacity of 5 groups
F=[z;1 1 1 1 1];
M=f*f ';
N=f*x ';
O=f*y ';
A= (m\n) ';
b= (m\o) ';
X1=a (1) *z+a (2);
Y1=b (1) *z+b (2);
Z1=z;
PLOT3 (X1,Y1,Z1, ' B ', x, Y, z, ' O ');
Note the function difference PLOT3!!!
When identifying a particular point, it can be identified using scatter or using scatter3 in three-dimensional space, with the following usage:
Scatter (x, y, ' o ');% two-dimensional space identifies points in X, y with an ' o ' symbol
scatter3 (x, y, z, ' O '); the identification of the midpoint of the three-dimensional space