【MATLAB】常用命令快速入门,国赛加油 (4)

image

scatter3 三维散点图 % scatter3 用法类似scatter x = linspace(0, 6*pi, 300); y = sin(x); z = cos(x); scatter3(x, y, z, 'o', 'MarkerEdgeColor','b',"MarkerFaceColor",'r');

image

mesh、surf 三维曲面

meshgrid 用法

x = 1:3; y = 1:5; [X,Y] = meshgrid(x,y)

image

% 绘制 z = x*e^(-(x^2+y^2)) % mesh 绘制的是网格 [x,y] = meshgrid(-10:1:10, -10:1:10); z = x.*exp(-x.^2-y.^2); mesh(x,y,z);

image

% 绘制 z = x*e^(-(x^2+y^2)) % surf 绘制的是曲面 [x,y] = meshgrid(-10:1:10, -10:1:10); z = x.*exp(-x.^2-y.^2); surf(x,y,z);

image

MATLAB文件读写 将数据写入文件 % writetable(m, filename):将m写入名为filename的文件 % 支持的文件扩展名:.txt .csv .xls .xlsm 或 .xlsx m = rand(4) + 1; m = round(m, 2, 'decimals'); % decimals:小数位数; significant:有效数字位数 t = table(m) m ____________________________ 1.51 1.38 1.94 1.59 1.82 1.81 1.88 1.21 1.79 1.53 1.55 1.3 1.64 1.35 1.62 1.47 writetable(t, 'm.txt',"Delimiter","\t","WriteVariableNames",false); % 相对路径,设置数据分隔方式为空格 % Delimiter(指定分隔符):默认","、"\t"、";"、"|" % WriteVariableNames:是否显示列名 writetable(t,'E:\Project\MATLABProject\process\m.txt'); % 绝对路径 writetable(t,'E:\Project\MATLABProject\process\m.csv'); writetable(t,'E:\Project\MATLABProject\process\m.xls'); writetable(t,'E:\Project\MATLABProject\process\m.xlsm'); writetable(t,'E:\Project\MATLABProject\process\m.xlsx'); type m.txt % 显示内容 1.51 1.38 1.94 1.59 1.82 1.81 1.88 1.21 1.79 1.53 1.55 1.3 1.64 1.35 1.62 1.47 % 将多个矩阵保存在同一个文件 "append" t2 = table(eye(4)) Var1 ________________ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % writeMode(写入模式):'overwrite'(覆盖,默认)、'append'追加 writetable(t2, 'm.txt',"Delimiter","\t","WriteVariableNames",false,"WriteMode","append"); type m.txt; 1.51 1.38 1.94 1.59 1.82 1.81 1.88 1.21 1.79 1.53 1.55 1.3 1.64 1.35 1.62 1.47 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 从文件读取数据 % t = readtable(filename) 从filename 文件中读取数据 % 支持的拓展名:.txt .csv .xls .xlsb .xlsm .xlsx .xltm .xltx t = readtable("m.txt") Var1 Var2 Var3 Var4 ____ ____ ____ ____ 1.51 1.38 1.94 1.59 1.82 1.81 1.88 1.21 1.79 1.53 1.55 1.3 1.64 1.35 1.62 1.47 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % table转化成数组 m = table2array(t) 列 1 至 3 1.5100 1.3800 1.9400 1.8200 1.8100 1.8800 1.7900 1.5300 1.5500 1.6400 1.3500 1.6200 1.0000 0 0 0 1.0000 0 0 0 1.0000 0 0 0 列 4 1.5900 1.2100 1.3000 1.4700 0 0 0 1.0000 % 读取excel表单 t = readtable('E:\Project\MATLABProject\process\m.xls') m_1 m_2 m_3 m_4 ____ ____ ____ ____ 1.51 1.38 1.94 1.59 1.82 1.81 1.88 1.21 1.79 1.53 1.55 1.3 1.64 1.35 1.62 1.47 % 通过名称获取excel表单 t_grade = readtable("student.xls","Sheet","grade") % 指定从student.xls的grade表单读取数据 Var1 ID Chinese Math English ____________ ____ _______ ____ _______ {'zhangsan'} 1001 98 94 95 {'lisi' } 1002 94 99 98 {'wangwu' } 1003 95 95 97 % 通过顺序获取excel表单 t_grade = readtable("student.xls","Sheet",1) % 指定从student.xls的第一个表单读取数据 Var1 ID Chinese Math English ____________ ____ _______ ____ _______ {'zhangsan'} 1001 98 94 95 {'lisi' } 1002 94 99 98 {'wangwu' } 1003 95 95 97 t_info = readtable("student.xls","Sheet",2) Var1 ID Height Weight ____________ ____ ______ ______ {'zhangsan'} 1001 126 54 {'lisi' } 1002 128 56 {'wangwu' } 1003 135 55 % 获取student.xls所有表单名称 sheets = sheetname("student.xls") "grade" "info" % 获取表单个数 length(sheets) ans = 2 % 指定单元格范围获取 readtable("student.xls","Range","B2:E4") Var1 Var2 Var3 Var4 ____ ____ ____ ____ 1001 98 94 95 1002 94 99 98 1003 95 95 97 table的更多用法 % table的构造 Names = {'zhangsan';'lisi';'wangwu'}; ID = {1001;1002;1003}; Chinese = {98;94;95}; Math = {94;99;95}; English = {95;98;97}; table(Names,ID,Chinese,Math,English) Names ID Chinese Math English ____________ ________ _______ ______ _______ {'zhangsan'} {[1001]} {[98]} {[94]} {[95]} {'lisi' } {[1002]} {[94]} {[99]} {[98]} {'wangwu' } {[1003]} {[95]} {[95]} {[97]} table(ID,Chinese,Math,English,'RowNames',Names) ID Chinese Math English ________ _______ ______ _______ zhangsan {[1001]} {[98]} {[94]} {[95]} lisi {[1002]} {[94]} {[99]} {[98]} wangwu {[1003]} {[95]} {[95]} {[97]} % 访问表格元素 % 1、通过索引(和矩阵一致) t_grade(1,2) t_grade(1,1:5) ; t_grade(1,:); % 获取第一行所有列 t_grade(:,[1,3]) % 获取所有行和第1、3列; % 2、通过列名获取 t_grade(:,"ID") ID ____ 1001 1002 1003 t_grade(:,{'Var1','Chinese'}) Var1 Chinese ____________ _______ {'zhangsan'} 98 {'lisi' } 94 {'wangwu' } 95 % 修改列名 t_grade.Properties.VariableNames 列 1 至 3 {'Var1'} {'ID'} {'Chinese'} 列 4 至 5 {'Math'} {'English'} t_grade.Properties.VariableNames(1) = {'Name'} Name ID Chinese Math English ____________ ____ _______ ____ _______ {'zhangsan'} 1001 98 94 95 {'lisi' } 1002 94 99 98 {'wangwu' } 1003 95 95 97 % 增加行 t_grade(4,:) = {'zhao',1004,98,95,100} Name ID Chinese Math English ____________ ____ _______ ____ _______ {'zhangsan'} 1001 98 94 95 {'lisi' } 1002 94 99 98 {'wangwu' } 1003 95 95 97 {'zhao' } 1004 98 95 100 % 增加列 t_grade.total = t_grade.Chinese + t_grade.Math + t_grade.English Name ID Chinese Math English total ____________ ____ _______ ____ _______ _____ {'zhangsan'} 1001 98 94 95 287 {'lisi' } 1002 94 99 98 291 {'wangwu' } 1003 95 95 97 287 {'zhao' } 1004 98 95 100 293 % 合并表格 t_grade = readtable("student.xls","Sheet","grade") Var1 ID Chinese Math English ____________ ____ _______ ____ _______ {'zhangsan'} 1001 98 94 95 {'lisi' } 1002 94 99 98 {'wangwu' } 1003 95 95 97 t_info = readtable("student.xls","Sheet","info") Var1 ID Height Weight ____________ ____ ______ ______ {'zhangsan'} 1001 126 54 {'lisi' } 1002 128 56 {'wangwu' } 1003 135 55 join(t_grade,t_info) Var1 ID Chinese Math English Height Weight ____________ ____ _______ ____ _______ ______ ______ {'zhangsan'} 1001 98 94 95 126 54 {'lisi' } 1002 94 99 98 128 56 {'wangwu' } 1003 95 95 97 135 55 % 创建student表单,并把数据写入 writetable(t_student, 'student.xls',"Sheet", 'student') 图像处理 图片的读写和显示 % 像素点的个数对应矩阵的大小 % 矩阵元素值的范围:0:255,0:黑色,255:白色 % uint8:unsigned int 8:无符号整型 % 彩色图片是 m*n*3 的RGB三通道矩阵 pic_bw = imread("JLU.jpg")

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwydsz.html