【 – 字数作文】
第一篇:《txt与matlab编程小刀笔记》
即使data.txt的数据格式是这样的也可以,需要转换。
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 ;
%程序为:
clc;clear;
load data.txt
data
AA=[reshape(data,3,length(data)/3)]'
m=4 %相当于你的2000
N=length(data)/m %相当于3000000/2000
for k=0:N/3-1
A(:,:,k+1)=AA((m*k+1):(m*k+m),:);%变为三维矩阵了。
end
A
运行结果:
data =
Columns 1 through 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Columns 22 through 36
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
AA =
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
m =
4
N =
9
A(:,:,1) =
1 2 3
4 5 6
7 8 9
10 11 12
A(:,:,2) =
13 14 15
16 17 18
19 20 21
22 23 24
A(:,:,3) =
25 26 27
28 29 30
31 32 33
34 35 36
matlab可以读 txt文件。使用 fopen,fread,fclose函数。关键是数据的格式,从而写相应的fread语句。
比如你的数据矩阵是
F=[160, 120; 159, 110];
那么讲该数据写入一个data文件是
filename='test.dat';
fid = fopen(filename,'w');
count = fwrite(fid,F','uchar');
fclose(fid);
将上述数据读出并存入2*2得矩阵是
filename='test.dat';
nSize=4;因为上述矩阵共有4个数据
nRow=2;
nColumn=2;
fid = fopen(filename,'rb');
temp = fread(fid, nSize, 'uchar');
fclose(fid);
result = reshape(temp, [nRow nColumn])';
%% 先打开文件
[filename,filepath]=uigetfile('*.txt','Select Input file');
file = [filepath filename];
fid = fopen(file,'rt');
if fid == -1
('Error opening the file')
end
%%读取文件
while 1
nextline = fgetl(fid); %读第一行
if ~isstr(nextline), break, end %读到最后跳出
disp(nextline);%这行可以不要
a = sscanf(nextline, ' %f %f ');%读取数据,根据你自己的需要改
end
end
导师让我做一个matlab曲线图。需要从txt文档中调用数据。
有3个txt文档,每个文档中都有很多组二维数据(x,y),每个文档中x的值不相同,但是都在同一段范围内。请问如何实现啊?
以下是数据:{1.65034E,19}.
txt1:
313.39 1.773E-19
313.54 1.813E-19
313.69 1.757E-19
313.84 1.675E-19
313.99 1.687E-19
314.14 1.744E-19
314.28 1.777E-19
314.43 1.776E-19
314.58 1.846E-19
314.73 2.004E-19
314.88 2.004E-19
txt2:
313.10 1.851E-19
313.25 1.831E-19
313.39 1.772E-19
313.54 1.757E-19
313.69 1.751E-19
313.84 1.696E-19
313.99 1.784E-19
314.14 1.797E-19
314.28 1.806E-19
314.43 1.826E-19
314.58 1.918E-19
314.73 1.920E-19
txt3:
313.10 2.005E-19
313.25 1.934E-19
313.39 1.890E-19
313.54 1.852E-19
313.69 1.865E-19
313.84 1.864E-19
313.99 1.929E-19
314.14 1.916E-19
314.28 1.951E-19
314.43 1.999E-19
314.58 2.124E-19
314.73 2.104E-19
314.88 2.139E-19
315.02 2.148E-19
315.17 2.125E-19
315.32 2.113E-19
315.47 2.118E-19
提问者: xiaochun5243 – 试用期 一级
最佳答案
>> load('d:\txt1.txt');
>> load('d:\txt2.txt');
>> load('d:\txt3.txt');
>> x1=txt1(:,1);
>> y1=txt1(:,2);
>> x2=txt2(:,1);
>> y2=txt2(:,2);
>> x3=txt3(:,1);
>> y3=txt3(:,2);
plot(x1,y1,x2,y2,'r',x3,y3,'k')
load命令一般是对*.dat的数据文件进行读取吧!
我一般导入*.txt格式的数据文件时是先打开workspace窗口,在这个窗口的顶部有个工具按钮“Load data file…”,通过这个打开你的filename.txt文件!这种方法肯定不会有错的,你可以试试看!
matlab读txt文件
2007-03-29 23:01
fid1=fopen('fx.txt','r');
%得到文件号
[f,count]=fscanf(fid,'%f %f',[12,90]);
%把文件号1的数据读到f中。其中f是【12 90】的矩阵
%这里'%f %f'表示读取数据的形势,他是按原始数据型读出
fclose(fid);
%关闭文件
第二篇:《light scattering data动态光散射》{1.65034E,19}.
2.455199e+02, 0.000000e+00, 0.000000e+00
2.472218e+02, 0.000000e+00, 0.000000e+00
2.489355e+02, 0.000000e+00, 0.000000e+00
2.506611e+02, 0.000000e+00, 0.000000e+00
2.523986e+02, 0.000000e+00, 0.000000e+00
2.541482e+02, 0.000000e+00, 0.000000e+00
2.559099e+02, 0.000000e+00, 0.000000e+00
2.576838e+02, 0.000000e+00, 0.000000e+00
2.594700e+02, 2.446660e+01, 7.349509e+00
2.612686e+02, 6.221881e+01, 2.603939e+01
2.630797e+02, 1.000000e+02, 5.607834e+01
2.649033e+02, 8.649644e+01, 8.206097e+01
2.667396e+02, 4.874842e+01, 9.670448e+01
2.685885e+02, 1.097082e+01, 1.000000e+02
2.704503e+02, 0.000000e+00, 1.000000e+02
2.723251e+02, 0.000000e+00, 1.000000e+02
2.742128e+02, 0.000000e+00, 1.000000e+02
2.761136e+02, 0.000000e+00, 1.000000e+02
2.780275e+02, 0.000000e+00, 1.000000e+02
2.799547e+02, 0.000000e+00, 1.000000e+02
2.818954e+02, 0.000000e+00, 1.000000e+02
2.838494e+02, 0.000000e+00, 1.000000e+02
2.858170e+02, 0.000000e+00, 1.000000e+02
2.877982e+02, 0.000000e+00, 1.000000e+02
2.897932e+02, 0.000000e+00, 1.000000e+02
2.918019e+02, 0.000000e+00, 1.000000e+02
2.938247e+02, 0.000000e+00, 1.000000e+02
2.979123e+02, 0.000000e+00, 1.000000e+02
2.999773e+02, 0.000000e+00, 1.000000e+02 3.020567e+02, 0.000000e+00, 1.000000e+02 3.041505e+02, 0.000000e+00, 1.000000e+02
3.062588e+02, 0.000000e+00, 1.000000e+02
1.000000e+00, 0.000000e+00, 0.000000e+00
1.307610e+00, 0.000000e+00, 0.000000e+00
1.709844e+00, 2.830923e+00, 8.057604e-01
2.235809e+00, 3.499770e+00, 1.801894e+00
2.923566e+00, 2.830923e+00, 2.607654e+00
3.822883e+00, 0.000000e+00, 2.607654e+00
4.998840e+00, 0.000000e+00, 2.607654e+00
6.536533e+00, 0.000000e+00, 2.607654e+00
8.547235e+00, 0.000000e+00, 2.607654e+00
1.117645e+01, 0.000000e+00, 2.607654e+00
1.461444e+01, 0.000000e+00, 2.607654e+00
1.910998e+01, 0.000000e+00, 2.607654e+00
2.498840e+01, 0.000000e+00, 2.607654e+00
3.267508e+01, 0.000000e+00, 2.607654e+00
4.272626e+01, 0.000000e+00, 2.607654e+00
5.586929e+01, 0.000000e+00, 2.607654e+00
7.305524e+01, 0.000000e+00, 2.607654e+00
9.552775e+01, 0.000000e+00, 2.607654e+00
1.249130e+02, 0.000000e+00, 2.607654e+00
1.633375e+02, 0.000000e+00, 2.607654e+00
2.135818e+02, 1.411220e+01, 6.624384e+00
2.792816e+02, 4.668941e+01, 1.991351e+01
4.775280e+02, 1.000000e+02, 7.406681e+01
6.244203e+02, 6.950243e+01, 9.384916e+01
8.164982e+02, 2.161008e+01, 1.000000e+02 1.067661e+03, 0.000000e+00, 1.000000e+02
1.396084e+03, 0.000000e+00, 1.000000e+02
1.825534e+03, 0.000000e+00, 1.000000e+02
2.387086e+03, 0.000000e+00, 1.000000e+02
3.121377e+03, 0.000000e+00, 1.000000e+02 4.081544e+03, 0.000000e+00, 1.000000e+02
5.337067e+03, 0.000000e+00, 1.000000e+02
3.457911e+02, 0.000000e+00, 0.000000e+00
3.488295e+02, 0.000000e+00, 0.000000e+00
3.518945e+02, 0.000000e+00, 0.000000e+00
3.549865e+02, 0.000000e+00, 0.000000e+00
3.581057e+02, 0.000000e+00, 0.000000e+00
3.612523e+02, 0.000000e+00, 0.000000e+00
3.644265e+02, 0.000000e+00, 0.000000e+00
3.676287e+02, 2.051005e+01, 6.230488e+00
3.708589e+02, 5.996116e+01, 2.444533e+01{1.65034E,19}.
3.741176e+02, 1.000000e+02, 5.482306e+01
3.774048e+02, 8.925565e+01, 8.193690e+01
3.807210e+02, 4.977151e+01, 9.705635e+01
3.840663e+02, 9.690145e+00, 1.000000e+02
3.874409e+02, 0.000000e+00, 1.000000e+02
3.908453e+02, 0.000000e+00, 1.000000e+02
3.942795e+02, 0.000000e+00, 1.000000e+02
3.977440e+02, 0.000000e+00, 1.000000e+02
4.047645e+02, 0.000000e+00, 1.000000e+02 4.083210e+02, 0.000000e+00, 1.000000e+02
4.119088e+02, 0.000000e+00, 1.000000e+02
4.155282e+02, 0.000000e+00, 1.000000e+02
4.191793e+02, 0.000000e+00, 1.000000e+02
4.228625e+02, 0.000000e+00, 1.000000e+02
4.265781e+02, 0.000000e+00, 1.000000e+02
4.303263e+02, 0.000000e+00, 1.000000e+02
4.341075e+02, 0.000000e+00, 1.000000e+02
4.379219e+02, 0.000000e+00, 1.000000e+02
4.417698e+02, 0.000000e+00, 1.000000e+02
4.456515e+02, 0.000000e+00, 1.000000e+02
4.495673e+02, 0.000000e+00, 1.000000e+02
4.535176e+02, 0.000000e+00, 1.000000e+02
4.575025e+02, 0.000000e+00, 1.000000e+02
4.525030e+02, 0.000000e+00, 0.000000e+00
4.559177e+02, 0.000000e+00, 0.000000e+00
4.593582e+02, 0.000000e+00, 0.000000e+00
4.628246e+02, 0.000000e+00, 0.000000e+00
4.663173e+02, 0.000000e+00, 0.000000e+00
4.698362e+02, 0.000000e+00, 0.000000e+00
4.733817e+02, 0.000000e+00, 0.000000e+00
4.769540e+02, 0.000000e+00, 0.000000e+00
4.805533e+02, 0.000000e+00, 0.000000e+00