Matlab 项目记录3
Matlab 知识点记录
1.动图Plot-Pause制作
fps=10;
flagHold=ishold();
xPath=[1:60;
1:60];
for i = 1:size(xPath,2)
plot(xPath(1,i), xPath(2,i),'b*');
pause(1/fps)
if 1==1 && ~flagHold
hold on
end
end
if ~flagHold
hold off
end
2.栅格图pcolor-colormap
a = zeros(20);
a(6:10,5:9)=1;
a(11:15,14:18)=1;
a(2:5,4:7)=1;
b=a;
b(end+1,end+1) = 1;
colormap([1 1 1;0 0 0]);% 创建颜色
pcolor(0:size(a,2),0:size(a,1),b); % 赋予栅格颜色
hold on
多色块区分作图
a = zeros(60);
b=a;
b(14:27,15:29)=1;
...
b(21:36,36:43)=1;
b(33:34,5:30)=3;
b(15:60,5:6)=3;
b(35:42,1:4)=2;
b(35:42,7:29)=2;
b=fliplr(b);
b=rot90(b);
b(end+1,end+1) = 1;
colormap([1 1 1;
0.5 0.3 0.3;
0.5 0.6 0.8;
0.7 0.7 0.7]);% 创建颜色
pcolor(0:size(a,2),0:size(a,1),b); % 赋予栅格颜色
hold on
3.路径显示path-point
function point_plot(start,path_point)
n=size(path_point,2);
for i=1:n
scatter(path_point(1,i)+0.5,path_point(2,i)+0.5,
'MarkerEdgeColor',[0 1 0],'MarkerFaceColor',[0 1 0], 'LineWidth',3);%path point
end
scatter(start(1)+0.5,start(2)+0.5,'MarkerEdgeColor',[1 0 0],
'MarkerFaceColor',[1 0 0], 'LineWidth',3);%start point
地图已经过变换处理,与part2不相同
4.路径环境图graphVector(8-direction)
NGrid=21;
grid.xx=linspace(0,NGrid-1,NGrid);
grid.yy=linspace(0,NGrid-1,NGrid);
zeros_grid = zeros(NGrid);
grid.F=obstacle_generation(zeros_grid);%obstacles generation
grid.F=~grid.F;
%function [graphVector]=grid2graph(grid)
%make sure values in F are logicals
grid.F=logical(grid.F);
%total number of nodes in the graph
NNodes=sum(grid.F(:));
%initialize stucture
graphVector=repmat(struct('neighbors',[],'neighborsCost',[],'x',[]),NNodes,1);
%grid size
szGrid=size(grid.F);
%this array keeps, for each non-false element of grid.F, the
%corresponding index in graphVector
%Note that to get the right ordering w.r.t. the definition of grid given in
%HW2, we need use transposes
idxMap=zeros(fliplr(size(grid.F)));
idxMap(grid.F')=1:NNodes;
idxMap=idxMap';
%fill in the x and neighbors fields
for iXCoord=1:szGrid(1)
for iYCoord=1:szGrid(2)
if grid.F(iXCoord,iYCoord)
idxGraph=idxMap(iXCoord,iYCoord);
graphVector(idxGraph).x=[grid.xx(iXCoord);grid.yy(iYCoord)];
idxNeighborsF=grid2graph_computeNeighbors(iXCoord,iYCoord,grid.F);
graphVector(idxGraph).neighbors=idxMap(sub2ind(szGrid,idxNeighborsF(1,:),idxNeighborsF(2,:)))';
end
end
end
%fill in the neighborcost field
%Note: we cannot do this in the previous loop because we need to have all
%the x fields populated first
for iNode=1:NNodes
graphVector(iNode).neighborsCost=...
sqrt(euclideanDistMatrix(graphVector(iNode).x,...
[graphVector(graphVector(iNode).neighbors).x]))';
end
5.文档写入读取fopen-fclose(转载)
1.文件的打开与关闭
write example
a=1;
b=2;
ends = fopen('Data/G_TP.txt','a');%keep writing
fprintf(ends,'%f, %f\n',a,b);
fclose(ends);
read example
fid=fopen('Data_For_Paper/Large_5_target/G_TP.txt','r');
f1=fscanf(fid,'%f, %f',[2,20]);
fclose(fid);
1)打开文件
在读写文件之前,必须先用fopen函数打开或创建文件,并指定对该文件进行的操作方式。fopen函数的调用格式为:
fid=fopen(文件名,‘打开方式’)
说明:其中fid用于存储文件句柄值,如果返回的句柄值大于0,则说明文件打开成功。文件名用字符串形式,表示待打开的数据文件。常见的打开方式如下:
‘r’:只读方式打开文件(默认的方式),该文件必须已存在。
‘r+’:读写方式打开文件,打开后先读后写。该文件必须已存在。
‘w’:打开后写入数据。该文件已存在则更新;不存在则创建。
‘w+’:读写方式打开文件。先读后写。该文件已存在则更新;不存在则创建。
‘a’:在打开的文件末端添加数据。文件不存在则创建。
‘a+’:打开文件后,先读入数据再添加数据。文件不存在则创建。
另外,在这些字符串后添加一个“t”,如‘rt’或‘wt+’,则将该文件以文本方式打开;如果添加的是“b”,则以二进制格式打开,这也是fopen函数默认的打开方式。
2)关闭文件
文件在进行完读、写等操作后,应及时关闭,以免数据丢失。关闭文件用fclose函数,调用格式为:
sta=fclose(fid)
说明:该函数关闭fid所表示的文件。sta表示关闭文件操作的返回代码,若关闭成功,返回0,否则返回-1。如果要关闭所有已打开的文件用fclose(‘all’)。
2.二进制文件的读写操作
1)写二进制文件
fwrite函数按照指定的数据精度将矩阵中的元素写入到文件中。其调用格式为:
COUNT=fwrite(fid,A,precision)
说明:其中COUNT返回所写的数据元素个数(可缺省),fid为文件句柄,A用来存放写入文件的数据,precision代表数据精度,常用的数据精度有:char、uchar、int、long、float、double等。缺省数据精度为uchar,即无符号字符格式。
例6.8 将一个二进制矩阵存入磁盘文件中。
>> a=[1 2 3 4 5 6 7 8 9];
>> fid=fopen('d:\test.bin','wb') %以二进制数据写入方式打开文件
fid =
3 %其值大于0,表示打开成功
>> fwrite(fid,a,'double')
ans =
9 %表示写入了9个数据
>> fclose(fid)
ans =
0 %表示关闭成功
2)读二进制文件
fread函数可以读取二进制文件的数据,并将数据存入矩阵。其调用格式为:
[A,COUNT]=fread(fid,size,precision)
说明:其中A是用于存放读取数据的矩阵、COUNT是返回所读取的数据元素个数、fid为文件句柄、size为可选项,若不选用则读取整个文件内容;若选用则它的值可以是下列值:N(读取N个元素到一个列向量)、inf(读取整个文件)、[M,N](读数据到M×N的矩阵中,数据按列存放)。precision用于控制所写数据的精度,其形式与fwrite函数相同。
3.文本文件的读写操作
1)读文本文件
fscanf函数可以读取文本文件的内容,并按指定格式存入矩阵。其调用格式为:
[A,COUNT]=fscanf(fid,format,size)
说明:其中A用来存放读取的数据,COUNT返回所读取的数据元素个数,fid为文件句柄,format用来控制读取的数据格式,由%加上格式符组成,常见的格式符有:d(整型)、f(浮点型)、s(字符串型)、c(字符型)等,在%与格式符之间还可以插入附加格式说明符,如数据宽度说明等。size为可选项,决定矩阵A中数据的排列形式,它可以取下列值:N(读取N个元素到一个列向量)、inf(读取整个文件)、[M,N](读数据到M×N的矩阵中,数据按列存放)。
2)写文本文件
fprintf函数可以将数据按指定格式写入到文本文件中。其调用格式为:
fprintf(fid,format,A)
说明:fid为文件句柄,指定要写入数据的文件,format是用来控制所写数据格式的格式符,与fscanf函数相同,A是用来存放数据的矩阵。
例6.9 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。
>> a='string';
>> fid=fopen('d:\char1.txt','w');
>> fprintf(fid,'%s',a);
>> fclose(fid);
>> fid1=fopen('d:\char1.txt','rt');
>> fid1=fopen('d:\char1.txt','rt');
>> b=fscanf(fid1,'%s')
b =
string
4.matlab读txt文件
fid=fopen('fx.txt','r');
%得到文件号
[f,count]=fscanf(fid,'%f %f',[12,90]);
%把文件号1的数据读到f中。其中f是[12 90]的矩阵
%这里'%f %f'表示读取数据的形势,他是按原始数据型读出
fclose(fid);
%关闭文件
另外有的txt文件还可以用load来打开
其语句为
f=load('fx.txt)
我个人觉得用第一种方式较好。因为有些时候,特别是fx.txt 的一行种有多个数据时用load就比较麻烦了
源:http://zhidao.baidu.com/link?url=5gTwwzI2P_OrLrhuLo78TLDQ67PP8q2HtJHmcfqZ0z66vBWyziyaYPg_WLoFX2tbpEcvef-veddl4m2i6QRnzq