MATLAB Application Notes

other

1. NaN value

MATLAB can directly use the function to judge whether the data is NaN :isnan()

3. Data analysis

1. Relevance

Mean, variance, covariance, standard deviation, correlation coefficient

mean()  %均值
nanmean()%去除NAN值求均值
var()   %方差
cov()   %协方差
std()   %标准差
corrcoef(B,b) %R² 相关系数

plot()  %绘图
rcoplot(r,rint)  %残差个案排序图,详情后见

Average of all non-NAN elements in a 2D array

AA = reshape(A, size(A,1)*size(A,2),1)%2维转1
A=nanmean(AA)

1.1 Scatter plot, line chart

See the back for drawing details: 2. Drawing

plot(x,y,'*',X,Y,'-')

1.2 Covariance and covariance matrix

Covariance is used to measure the overall error of two variables. If the change trend of the two variables is consistent, the covariance is positive, indicating that the two variables are positively correlated. If the two variables have opposite trends, the covariance is negative, indicating that the two variables are negatively correlated. If the two variables are independent of each other, then the covariance is 0, indicating that the two variables are not related.
insert image description here

1.3 Correlation coefficient

The correlation coefficient (Correlation coefficient) is a statistical indicator of the close relationship between the response variables, and the value range of the correlation coefficient is between 1 and -1. 1 indicates that the two variables are completely linearly correlated, -1 indicates that the two variables are completely negatively correlated, and 0 indicates that the two variables are not correlated. The closer the data is to 0, the weaker the correlation is.
R

standard deviation
covariance

2. Analysis test

2.1 Univariate regression and multiple regression, residual graph analysis

Regression analysis is a statistical method to determine the relationship between two or more groups of variables.

insert image description here
insert image description here
insert image description here

B=data(:,3);
B=B';
b=data(:,6);
b=b';
X = [ones(length(b),1), B'];%x'表示行向量转置为列向量
Y = b';
%根据输入参数y与X,用最小二乘法求线性回归系数b
[ b,bint,r,rint,stats ] = regress(Y,X);
rcoplot(r,rint)  %残差个案排序图
a=b(1)+(b(2))*B
plot(B,Y,'*',B,a,'-')  %残差点线图

2.2 Significance level p-value

[r,p]=corr(yy',xx');  %p为显著性水平
%[h,p,ci,stats1] = ttest2(yy,xx);

1. Reading, writing and storage of file data in different formats

1. Commonly used

1.1 .xls、.xlsx

Write p into the table path, read it as p, and read the string in xls

xlswrite ('D:\study\AOD\2020beijing\data.xls', P);
p=xlsread('D:\study\AOD\2020beijing\data.xls');
p=xlsread('D:\study\AOD\2020beijing\data.xls','Sheet1');%sheet1子表格名
[~,~,raw]=xlsread('D:\study\CE-318\a指数550_2021_1.xlsx','Sheet2')%raw获取包括字符串内容
data=raw(:,:);

1.2 .mat

Save as Excel data

(1) Save the data in mat format in matlab as an xls file, the command is:

xlswrite('训练_42.xlsx',sounds_y2);
%其中“训练_42.xlsx”为另存为文件的名称,sounds_y2为原mat格式的数据

(2) Read the Excel data into matlab, the command is:

testlabel=xlsread('testlabel.xlsx');

2. Professional

.tiff、.tif

[A,R] = geotiffread('file path')
A: data
R: attributes

[A,R] = geotiffread('D:\study\AOD\AVR\2021002.tif') 

%%%多波段
filepath='D:\study\AOD\AVR\2021002.tif';                     %%图像名称与路径
Info=imfinfo(filepath);                                      %%获取图片信息
Slice=size(Info,1);                                          %%获取图片z向帧数
Width=Info.Width;
Height=Info.Height;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Image=zeros(Height,Width,Slice);
for i=1:Slice
    Image(:,:,i)=imread(filepath,i);                         %%一层一层的读入图像
end

.hdr

The syntax rules of this function are:
X = multibandread(filename, size, precision, offset, interleave, byteorder)
filename : filename
size : image size and band number, size = [row number, column number, band number]
precision : the read image Data format, such as 'uint8', 'uint16', 'double', etc.
offset : offset (I still don't understand this parameter very well)
interleave : the data format of the stored image, there are three formats of bsq, bil, and bip
byteorder : The byte arrangement of data storage, there are 'ieee-le' (little endian), 'ieee-be' (big endian)

hdr = read_envihdr('D:\study\AOD\data1\MYD04_L2.A2021001.0900.061.20210_Swath_2D_1_georef.hdr')
Image = multibandread('D:\study\AOD\data1\MYD04_L2.A2021001.0900.061.20210_Swath_2D_1_georef.dat',hdr.size,[hdr.format '=>single'],hdr.header_offset,hdr.interleave,hdr.machine);
% A=Image(:,:,1);
% B=Image(:,:,2);
% C=Image(:,:,3);
A=Image(:,:,4);

.nc

ncFilePath='D:\Alice\研\modis,matlab\data\MYD04_L2.A2020354.0435.061.2020357193838.nc';
%路径
ncdisp(ncFilePath,'/','full');
lon=ncread(ncFilePath,'lon');  %经纬度
lat=ncread(ncFilePath,'lat');

%% 显⽰结构
% ncdisp(ncFilePath);%在命令⾏窗⼝中以⽂本形式显⽰ NetCDF 数据源 source 中的所有组、维度、变量定义,以及所有属
性。
% ncdisp(ncFilePath,‘evap’);%显⽰指定变量的内容.
% ncdisp(ncFilePath,/,‘min’);%只显⽰⽰例⽂件 example.nc 的组层次结构和变量定义。
% ncdisp(ncFilePath,/,‘full’);%全部显⽰所有结构和定义信息
%% 读取变量值
% ncid = netcdf.open(ncFilePath,‘NOWRITE’); %打开nc⽂件返回索引ID
% [ndims,nvars,ngglobalatts,unlimdimid] = netcdf.inq(ncid);%获取维数,变量数,全局属性数量,
% [varname,xtype,dimids,natts] = netcdf.inqVar(ncid,0); %根据变量索引号获取变量的名称
lon=ncread(ncFilePath,‘lon’);%读取经度变量
lat=ncread(ncFilePath,‘lat’);%读取纬度变量
sm_data=ncread(ncFilePath,‘sm’);%读取sm变量
sm_data1=sm_data(1174:1194,240:262);
sm_data1(sm_data10)=[];
aa=sm_data1;
[h,l]=find(aa0);
aa(h,l)=[];
%% 显⽰数据
% pcolor(lat,lon,sm_data);pcolor(X,Y,C) 指定顶点的 x 坐标和 y 坐标,C 的⼤⼩必须与 x-y 坐标⽹格的⼤⼩匹配,例如,如果
X 和 Y 定义⼀个 m×n ⽹格,则 C 必须为 m×n 矩阵.
% [x,y]=meshgrid(lon,lat);%根据经纬度信息产⽣格⽹.
% phandle=pcolor(x,y,sm_data’);%显⽰⼀个矩阵,其中x,y,sm_data的⾏列数必须⼀致。类似surface函数.
% colorbar
% imwrite(sm_data1,‘D:\Alice\研\modis\aa.tif’,‘tif’)
%% 保存为地理栅格tif格式,以便ArcGIS读取.
data=flipud(sm_data1);
R = georasterref(‘RasterSize’, size(data),‘Latlim’, [double(min(lat)) double(max(lat))], ‘Lonlim’,
[double(min(lon)) double(max(lon))]);%地理栅格数据参考对象() 
geotiffwrite(‘D:\Alice\研\modis\aa.tif’,data,R);

3. Batch reading

csv_path=  'D:\study\CE-318\北京-CAMS(39.933牛顿,116.317牛顿)\';   %文件夹路径 
path_list = dir(strcat(csv_path,'*.csv'));  
%dir 函数 列出当前目录下所有子文件夹和文件%

list_num = length(path_list);%%文件数量
for i=1:list_num
%%%%%
end

3.1 Batch read with string csv

Reference: https://blog.csdn.net/qq_41661878/article/details/119330873

uiopen('D:\study\CE-318\beijingN39.977,E116.381\data\20170101_20171231_Beijing.csv',1)

4. Batch storage (custom naming)

xlswrite(strcat(csv_path,yearname,'.xlsx'),DATA); 
%自定义命名将DATA写入表格
%csv_path路径yearname名字

2. Drawing

1. Graphics

title('图形名称') %(都放在单引号内)
xlabel('x轴说明')
ylabel('y轴说明')
text(x,y,'图形说明','fontsize',16)
legend('图例1','图例2',)
set(gca,‘XTickLabel’,[1991:1:2009]);%给X轴坐标加标签 (1991-2009间隔1xtickangle(50)%更改x轴标签角度

2. Coordinate control

axis equal: The vertical and horizontal axes adopt equal-length scales.
axis square: Generate a square coordinate system (rectangular by default)
axis auto: Use the default setting
axis off: Cancel the coordinate axis
axis on: Display the coordinate axis
axis([xmin xmax ymin ymax])
command You can limit the x and y coordinates of the two-dimensional image, here enter axis([0 2 pi -2 2]), limit the abscissa to 0 to 2 pi, and the ordinate to -2 to 2.

3. Legend comment

legend(‘text1‘, ‘text2‘,)

4. Text Notes

Add text annotation anywhere in the graph, the function used is text. Its calling syntax is text(x,y,'text'), where x and y are the coordinate positions of the marked points, and text is the added text annotation.
text(x, y, 'label name') or text(x, y, z, 'label name')
Matlab drawing basis - to add text description to the image (text object)

5. Line type

exampleplot(x,y,'k*',x1,y1,'b--')
insert image description here

How to set custom and rotating axis scale values ​​and label references in matlab

%Set the x-axis range and scale
set(gca,'XLim',[0 10]);%X-axis data display range
set(gca,'XTick',[0:1:10]);%Set the coordinates to be displayed Scale
set(gca,'XTickLabel',[0:1:10]);% Label the coordinates

Use plot() to draw a graph, such as we enter the code in the MATLAB command window:
a= linspace(-15,0.1,15); % coordinate scale -15~15, spacing 0.1
b=sin(a);
plot(a ,b)
will draw a sine function curve with abscissa -15 to 15.

Use the xticks() function, xticklabels() function, and yticks() function to display the abscissa and ordinate scale values ​​at the specified position , and the spacing can be inconsistent.
xticks([-3 pi -2 pi -pi 0 pi 2 pi 3 pi])
xticklabels({'-3\pi','-2\pi','-\pi','0','\pi' ,'2\pi','3\pi'})
yticks([-1 -0.9 -0.5 0 0.4 0.7 1])

Rotate the angle of the abscissa and ordinate labels or scales, use the xtickangle() function and the ytickangle() function
a= 511 rand(1,33);
b=rand(1,33);
scatter(a,b,'r
' )
xtickangle(50)
ytickangle(80)
insert image description here

6. Color

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44083023/article/details/130247206