本文介绍了如何使用MATLAB来分隔文本文件的行。例如,第一组中的1-3(模式)在第二组中的1-3中,以此类推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想使用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 37642 -4177 37632 -4177 37602 -4173 37582 -4171 37572 -4170 37582 -4171 37542 -4176 37492 -4176 37522 -4179 37582 -4182 37692 -4195 37852 -4221 38032 -4251 38332 -4276 38662 -4302 38992 -4321 39262 -4341 39492 -4360 39612 -4375 39652 -4384 39652 -4389 39622 -4386 39592 -4389 39582 -4390 39562 -4390 39582 -4387 39622 -4392 39652 -4381 39553 -12851 -128511 -4396 37792 -4396 37782 -4398 37752 -4396 37752 -4396 37782 -4393 37872 -4387 37962 -4371 38082 -4338 38322 -4297 38662 -4257 39022 -4225 39342 -4207 39502 -4195 39592 -4192 39592 -4189 39562 -4189 39552 -4192 39492 -4188 39492 -4183 39492 -4183 39493 -12851 -12851How 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 filefid = 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 3n = 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中,以此类推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 21:16