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开发者_JAVA技巧 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.
Answer to the original text file of the format
1 -4177 3764
2 -4177 3763
2 -4177 3760
2 -4173 3758
2 -4171 3757
Assuming your file is called 'text.txt' and located on drive C, do:
[col1, col2, col3] = textread('C:\text.txt', '%d %d %d');
all your entries from the text file will be in the corresponding variables col1, col2 and col3.
EDIT:
cells = cell(1, numel(col1));
for j = 1 : numel(col1)
cells{j} = [col1(j) col2(j) col3(j)];
end
This will group your values as follows:
cells{1}: 1 -4177 3764
cells{2}: 2 -4177 3763
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
精彩评论