У меня есть файл с именем data.dat
с содержимым (образец):
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
.
.
.
Я пытаюсь объединить каждый массив из 3 строк в один массив строк в конце, чтобы получить матрицу f x 29
:
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
.
.
.
, а затем сместите 10-й и 11-й столбцы в 1-й и 2-й ряд:
E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
Как мне это сделать в MATLAB? Вот моя попытка, но она неверна.
% Find out number of rows in file
rline=0;
x=0;
% Open Data File
fid = fopen('data.dat','rt');
% Loop through data file until we get a -1 indicating EOF
while(x~=(-1))
x=fgetl(fid);
rline=rline+1;
end
rline = rline-1;
% How many row in final file
fline=rline/3; % one row in final file represent by 3 rows from raw data
% Create 3 seperate matrix named as z1,z2,z3
frewind(fid);
for i = 1:rline
num1 = fscanf(fid,'%f %f %f %f %f %f %f %f %f\n')'; % Read in numbers
name1 = fscanf(fid,'%s %s',rline); % Filter out string at end of line
if(i==1)
result1 = num1; % Add 1st row
names1 = name1; % Add 1st text string
else
result1 = [result1;num1]; % Add additional rows
names1 = char(names1,name1); % Add next string
names1 = names1';
end
i=i+3;
end
fclose(fid);
z1 = result1;
zname= names1;
frewind(fid);
for i = 2:rline
num2 = fscanf(fid,'%f')'; % Read in numbers
if(i==2)
result2 = num2; % Add 2nd row
else
result2 = [result2;num2]; % Add additional rows
end
i=i+3;
end
fclose(fid);
z2 = result2;
frewind(fid);
for i = 3:rline
num3 = fscanf(fid,'%f')'; % Read in numbers
if(i==3)
result3 = num3; % Add 3rd row
else
result3 = [result3;num3]; % Add additional rows
end
i=i+3;
end
fclose(fid);
z3 = result3;
% Create a final data matrix of F = (fline x 29)
for i = 1: fline
for j = 1: fline
F(i, [1:2]) = zname(j,:);
F(i, [3:11]) = z1(j,:);
F(i, [12:20]) = z2(j,:);
F(i, [21:29]) = z3(j,:);
j=j+1;
end
i=i+1;
end
Final_data = [F];