Matlab代码备忘

1.Matlab读取和写入文件 

1 %读取数据
2 name=strcat('ptsinterval90_00000',num2str(0),'.txt');
3 fid=fopen(name,'r');
4 sizeA=[4 Inf];
5 A =fscanf(fid,'%f %f %f %f',sizeA);
6 fclose(fid);

写入文件

1
2
3
4
5
6
7
8
set(hp1,'xdata',bbb(1,:),'ydata',bbb(2,:),'zdata',bbb(3,:));
M=size(bbb,2);
name=strcat('laser_',num2str(i),'.txt');
fid=fopen(name,'a+');
for kk =1:M
    fprintf(fid,'%g %g %g\r\n',bbb(1,kk),bbb(2,kk),bbb(3,kk));
end
fclose(fid);

保存矩阵

复制代码
 1 function [ flag ] = SaveMatrix( filename, mat,type )
 2 %SAVEMATRIX 此处显示有关此函数的摘要
 3 %   此处显示详细说明
 4 % type 1 数字
 5 % type 2 字符
 6 fid=fopen(filename,'wt');%写入文件路径 
 7 [m,n]=size(mat); 
 8 for i=1:1:m 
 9     for j=1:1:n 
10         if j==n 
11             if (type==1)
12                  fprintf(fid,'%g\n',mat(i,j)); 
13             elseif (type==2)
14                  fprintf(fid,'%s\n',mat(i,j)); 
15             end
16         else 
17              if (type==1)
18                  fprintf(fid,'%g\t',mat(i,j)); 
19              elseif (type==2)
20                  fprintf(fid,'%s\t',mat(i,j)); 
21             end
22         end 
23     end 
24 end 
25 fclose(fid);
26 flag=1;
27 end
复制代码

 

2.强制刷新

drawnow;%强制刷新

3.动态增加数组

curR=[R t;[0 0 1]];

1
2
lineSeg=[1;1];
lineSeg=[lineSeg [1; 1]];

4.线段拟合函数

polyfit

5.非线性最小二乘优化

lsqnonlin

6.绘制包围盒 http://www.mathworks.com/matlabcentral/fileexchange/54463-drawboundingbox3d-xbnd-ybnd-zbnd-linewidth-color-/content/drawBoundingBox3d.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function  DrawBoundingBox(xBnd,yBnd,zBnd,lineWidth,color)
% DrawBoundingBox(xBnd,yBnd,zBnd,lineWidth,color)
%
% This function draws the wireframe box that as described by the limits in
% xBnd, yBnd, and zBnd
%
% INPUTS:
%   xBnd = [xLow, xUpp]
%   yBnd = [yLow, yUpp]
%   zBnd = [zLow, zUpp]
%
 
hold on;
 
% Draw the bottom:
plot3(...
    xBnd([1,1,2,2,1]),...
    yBnd([1,2,2,1,1]),...
    zBnd([1,1,1,1,1]),...
    'LineWidth',lineWidth','color',color);
 
% Draw the top:
plot3(...
    xBnd([1,1,2,2,1]),...
    yBnd([1,2,2,1,1]),...
    zBnd([2,2,2,2,2]),...
    'LineWidth',lineWidth','color',color);
 
% Draw the sides:
plot3(...
    xBnd([1,1]),...
    yBnd([1,1]),...
    zBnd([1,2]),...
    'LineWidth',lineWidth','color',color);
plot3(...
    xBnd([1,1]),...
    yBnd([2,2]),...
    zBnd([1,2]),...
    'LineWidth',lineWidth','color',color);
plot3(...
    xBnd([2,2]),...
    yBnd([2,2]),...
    zBnd([1,2]),...
    'LineWidth',lineWidth','color',color);
plot3(...
    xBnd([2,2]),...
    yBnd([1,1]),...
    zBnd([1,2]),...
    'LineWidth',lineWidth','color',color);
 
end

7.matlab中小数取整的函数大约有四个:floor、ceil、round、fix

8.plot3函数绘制点的大小

1
plot3(model(1,:),model(2,:),model(3,:),'r.',data(1,:),data(2,:),data(3,:),'b.','MarkerSize',1), hold on, axis equal

9.运行时间计算

tic;

time=toc;

10.绘制箱线图

11.matlab plot函数绘制点符号

  • 符号

[ + | o | * | . | x | square | diamond | v | ^ | > | < | pentagram | hexagram | - | : ]

"+":"+"形线
"o":"o"形线
"*":"*"形线
".":"."形线
"v":"v"形线
"^":"^"形线
">":">"形线
"<":"<"形线
"square": 正方形
"pentagram": 五角形
"hexagram": 六角形
"-": 实线
":": 虚线
  • 颜色
[ y | m | c | r | g | b | w | k ]
y: 黄色
m: 粉红
c: 亮蓝
r: 大红
g: 绿色
b: 蓝色
w: 白色
k: 黑色

 12.set(gcf,'Color',[1,1,1]); % 修改背景色

posted @   太一吾鱼水  阅读(618)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示