I have a file named data.dat with contents (sample):
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
.
.
.
I’m trying to merge every 3 rows array into one row array at the end to give a matrix of 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
.
.
.
and then shift the 10th and 11th column to the 1st and 2nd row:
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
How do I do this in MATLAB? Here is my attempt, but it is incorrect.
% 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];
I am using the sample data file you’ve given as an example. The TEXTSCAN function is used to parse the file
data.dat
MATLAB code
Note that since the data contains heterogeneous types (numeric and characters), we read and store them separately. The final line of code shows one way of merging all data into a single cell array: