matlab处理txt文件数据
read_txtfile.,m
clear
close all
clc
%load函数一般将用来导入纯数字的文件,可以是文本格式的文件或者是matlab保存的mat格式的文件
position=load('坐标点.txt'); %将.txt数据读入到matlab工作空间
[m,n]=size(position); %获得数据矩阵的大小
j=1;
sumx=0;
sumy=0;
for i=1:10
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/10;ave(j,2)=sumy/10; %求平均值并存在ave中
j=j+1;
sumx=0;
sumy=0;
for i=11:19
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/9;ave(j,2)=sumy/9;
j=j+1;
sumx=0;
sumy=0;
for i=20:24
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/5;ave(j,2)=sumy/5;
j=j+1;
sumx=0;
sumy=0;
for i=25:34
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/10;ave(j,2)=sumy/10;
j=j+1;
sumx=0;
sumy=0;
for i=35:41
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/7;ave(j,2)=sumy/7;
j=j+1;
sumx=0;
sumy=0;
for i=42:47
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/6;ave(j,2)=sumy/6;
j=j+1;
sumx=0;
sumy=0;
for i=48:53
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/6;ave(j,2)=sumy/6;
j=j+1;
sumx=0;
sumy=0;
for i=54:58
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/5;ave(j,2)=sumy/5;
j=j+1;
sumx=0;
sumy=0;
for i=59:63
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/5;ave(j,2)=sumy/5;
j=j+1;
sumx=0;
sumy=0;
for i=64:68
sumx=sumx+position(i,1);
sumy=sumy+position(i,2);
end
ave(j,1)=sumx/5;ave(j,2)=sumy/5;
save('ave.txt','ave','-ASCII'); %将变量ave的数据写入ave.txt文件并保存,使用ASCII格式
save('test.txt','-ASCII') %把当前工作空间的所有变量保存到test.txt文件中
ave变量数据
坐标点.txt数据
264,715
258,719
274,728
264,728
254,728
257,733
260,731
262,733
268,733
270,739
225,605
223,598
210,605
220,610
223,615
209,615
230,620
220,622
205,618
168,538
168,542
164,544
168,545
174,544
210,455
180,455
175,452
170,453
185,460
178,460
190,470
183,473
175,472
180,476
120,400
119,388
112,394
125,410
114,405
116,410
113,416
96,304
88,305
100,312
93,311
86,310
94,315
10,451
11,449
13,450
16,450
12,453
15,455
162,660
161,659
159,659
160,657
164,658
110,561
110,563
110,565
109,567
112,568
105,473
106,471
103,473
107,475
104,477