如何使用MATLAB来分隔文本文件的行。例如,第一组中的1-3(模式)在第二组中的1-3中,以此类推 [英] How to separate lines of a text file using MATLAB.like first 1-3(modes) in one group next 1-3 in second group and so on
问题描述
我想使用MATLAB来分隔下面的文本文件的部分:像第一组1-3中的前1-3(模式),第二组1-3中的其他模式,等等...
编辑:文件格式自从最初发布以来已被编辑
)(-x)(y)
1 -4177 3764
2 -4177 3763
2 -4177 3760
2 -4173 3758
2 -4171 3757
2 -4170 3758
2 -4171 3754
2 -4176 3749
2 -4176 3752
2 -4179 3758
2 -4182 3769
2 -4195 3785
2 -4221 3803
2 -4251 3833
2 -4276 3866
2 -4302 3899
2 -4321 3926
2 - 4341 3949
2 -4360 3961
2 -4375 3965
2 -4384 3965
2 -4389 3962
2 -4386 3959
2 -4389 3958
2 -4390 3956
2 -4390 3958
2 -4387 3962
2 -4392 3965
2 -4381 3955
3 -12851 -12851
1 -4396 3779
2 -4396 3778
2 -4398 3775
2 -4396 3775
2 -4396 3778
2 -4393 3787
2 -4387 3796
2 -4371 3808
2 -4338 3832
2 -4297 3866
2 -4257 3902
2 -4225 3934
2 -4207 3950
2 -4195 3959
2 -4192 3959
2 -4189 3956
2 -4189 3955
2 -4192 3949
2 -4188 3949
2 -4183 3949
2 -4183 3949
3 -12851 -12851
我应该怎么做?
谢谢。 / p>
以下是使用 TEXTSCAN 功能。它读取文件,然后使用 MAT2CELL 分隔每3行,并把在这种情况下,你有52行数据(加上一行头被忽略),它们不能被3整除,因此最后一项只有一行。
%#读文件
fid = fopen('file.dat , 'R');
C = textscan(fid,'%f%f%f',...
'Delimiter','','HeaderLines',1,'CollectOutput',true);
fclose(fid);
C = C {1};
%#处理行数不能被3
n = fix(size(C,1)/ 3)* 3整除的情况。
CC = mat2cell(C(1:n,:),repmat(3,1,n / 3),size(C,2));
CC {end + 1} = C(n + 1:end,:);
结果:
>> whos CC
名称大小字节类属性
CC 18x1 2328单元
<
>> CC {end-1}
ans =
2 -4188 3949
2 -4183 3949
2 -4183 3949
>> CC {end}
ans =
3 -12851 -12851
I would like to separate the parts of the following text file using MATLAB:like first 1-3(modes) in one group next 1-3 in second group and so on...
edit: the file format has been edited since the initial post
(modes) (-x) (y) 1 -4177 3764 2 -4177 3763 2 -4177 3760 2 -4173 3758 2 -4171 3757 2 -4170 3758 2 -4171 3754 2 -4176 3749 2 -4176 3752 2 -4179 3758 2 -4182 3769 2 -4195 3785 2 -4221 3803 2 -4251 3833 2 -4276 3866 2 -4302 3899 2 -4321 3926 2 -4341 3949 2 -4360 3961 2 -4375 3965 2 -4384 3965 2 -4389 3962 2 -4386 3959 2 -4389 3958 2 -4390 3956 2 -4390 3958 2 -4387 3962 2 -4392 3965 2 -4381 3955 3 -12851 -12851 1 -4396 3779 2 -4396 3778 2 -4398 3775 2 -4396 3775 2 -4396 3778 2 -4393 3787 2 -4387 3796 2 -4371 3808 2 -4338 3832 2 -4297 3866 2 -4257 3902 2 -4225 3934 2 -4207 3950 2 -4195 3959 2 -4192 3959 2 -4189 3956 2 -4189 3955 2 -4192 3949 2 -4188 3949 2 -4183 3949 2 -4183 3949 3 -12851 -12851
How should I go about doing this?
Thanks.
Here is an example code using the TEXTSCAN function. It read the file, then it separates every 3 lines using MAT2CELL and put the result in a cell-array.
Note that in your case, you have 52 lines of data (plus one header line ignored), which are not divisible by 3, thus the last entry will only have one line.
%# read file
fid = fopen('file.dat','r');
C = textscan(fid, '%f %f %f', ...
'Delimiter',' ', 'HeaderLines',1, 'CollectOutput',true);
fclose(fid);
C = C{1};
%# handles the case where number of lines is not divisible by 3
n = fix(size(C,1)/3)*3;
CC = mat2cell(C(1:n,:), repmat(3,1,n/3), size(C,2));
CC{end+1} = C(n+1:end,:);
The result:
>> whos CC
Name Size Bytes Class Attributes
CC 18x1 2328 cell
and the last two cells:
>> CC{end-1}
ans =
2 -4188 3949
2 -4183 3949
2 -4183 3949
>> CC{end}
ans =
3 -12851 -12851
这篇关于如何使用MATLAB来分隔文本文件的行。例如,第一组中的1-3(模式)在第二组中的1-3中,以此类推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!