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);

写入文件

    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]];

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

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函数绘制点的大小

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 @ 2016-06-21 21:14  太一吾鱼水  阅读(618)  评论(0编辑  收藏  举报