MATLAB大纲
- 基础语法
基本类型
- 高级类型
- 单元数组
{...}
(元素可以是不同的类型)
- 结构数组/结构体
args.x
- 表、时间表
- 其他:稀疏矩阵、字典、分类数组
程序结构
- 文件读写
- 错误和调试
矩阵运算(张量运算:多维数组)
文件读写
mat文件
datas.mat
,变量A
nc文件
clc; clear; format default path = 'cmems_mod_glo_phy_anfc_0.083deg_PT1H-m_1737117424162.nc'; ncdisp cmems_mod_glo_phy_anfc_0.083deg_PT1H-m_1737117424162.nc; vars = {ncinfo(path).Variables.Name}
time = datetime(1970,1,1) + ncread(path,'time')./(24*3600); la = double(ncread(path,'latitude')); lo = double(ncread(path,'longitude')); temp = ncread(path,'thetao'); temp = temp(:,:,1,1)
latlim = [min(la)-1, max(la)+1]; lonlim = [min(lo)-1, max(lo)+1];
clf
axesm('MapProjection','robinson','MapLatLimit', latlim, 'MapLonLimit', lonlim);
pcolorm(la, lo, temp');
shading interp
framem('FlineWidth',2,'FEdgeColor','k')
tightmap
colormap(jet(32));
clim([-32,32])
colorbar('ytick' ,-32:2:32,'fontsize',15)
axis off
load coastlines
geoshow(coastlat , coastlon , 'color', 'k')
text(2.65,1.5,'\circC','FontSize', 30)
|
cmems_mod_glo_phy_anfc_0.083deg_PT1H-m_1737117424162.nc
csv/xlsx文件
writematrix(A,'datas.xlsx',Sheet='mysheet');
sheets = sheetnames('datas.xlsx') B = readmatrix('datas.xlsx',Sheet='Sheet1');
|
错误和调试
异常抛出
error('This is an error!') assert(cond,msg) warning('This is an warning!')
|
异常处理
try 语句块 catch [异常变量] 响应语句块 end
|
高级类型
单元数组/元胞数组/Cell
寻访数组
穿透寻访
C{2,2}
得到元素
x=[C{2,:}]
多元素穿透寻访
浅寻访
类型转换
A = cell2mat(C)
拼接
C = mat2cell(A,[2 2]行,[3 2]列)
(必须指定)分割/分块矩阵
c = num2cell(a)
分成单个元素
C = cellstr(A)
字符串数组(不补空白符)
结构体
稀疏矩阵
sparse(A)
将矩阵A
转换为稀疏矩阵(仅支持矩阵)