Read all lines of binary file at once
Hello! My code currently opens 120 folders one by one and each folder contains 2 binary files. The code then opens each binary file one by one and reads it. These binary files have 1250 lines each and the lines are read once again one by one.
Is it possible to have the code read all of the lines of 1 binary file at once, not go through 1250 lines sequentially?
The format of the first line is ‘int int int int int int’ (6x int) and the format of all other lines is ‘int char float float float float float float’ (1x int, 1x char, 6x float). The code can be found below.
If there are other suggestions for speeding up the code, please do mention them.
listing = dir(ResultsFolder); % Folder with all 120 folders
for i = 1:length(listing) % Goes from 1 to 120
FieldFolder = [listing(i).folder ” listing(i).name]; % Open 1 of the 120 folders
listingFields = dir(FieldFolder);
FieldFiles = listingFields(contains({listingFields.name},sprintf(‘%A.fld’))); % Get the 2 binary files of interest
for j = 1:length(FieldFiles) % Goes from 1 to 2
fileID = fopen([FieldFolder ” FieldFiles(j).name]); % Open binary file
k = 1;
fileEnd = false;
while ~fileEnd % Read all of the 1250 lines
% FirstLineFormat=’int int int int int int’
% OtherLineFormat=’int char float float float float float float’
try
if k == 1
FieldData_tmp(k,:) = [fread(fileID, 6, ‘int’)’ 0 0 ]; % Read first line
else
FieldData_tmp(k,:) = [fread(fileID, 1, ‘int’) fread(fileID, 1, ‘char’) fread(fileID, 6, ‘float’)’]; % Read other lines
end
catch
fileEnd = true; % No lines left
end
k = k+1;
end
fclose(fileID);
end
endHello! My code currently opens 120 folders one by one and each folder contains 2 binary files. The code then opens each binary file one by one and reads it. These binary files have 1250 lines each and the lines are read once again one by one.
Is it possible to have the code read all of the lines of 1 binary file at once, not go through 1250 lines sequentially?
The format of the first line is ‘int int int int int int’ (6x int) and the format of all other lines is ‘int char float float float float float float’ (1x int, 1x char, 6x float). The code can be found below.
If there are other suggestions for speeding up the code, please do mention them.
listing = dir(ResultsFolder); % Folder with all 120 folders
for i = 1:length(listing) % Goes from 1 to 120
FieldFolder = [listing(i).folder ” listing(i).name]; % Open 1 of the 120 folders
listingFields = dir(FieldFolder);
FieldFiles = listingFields(contains({listingFields.name},sprintf(‘%A.fld’))); % Get the 2 binary files of interest
for j = 1:length(FieldFiles) % Goes from 1 to 2
fileID = fopen([FieldFolder ” FieldFiles(j).name]); % Open binary file
k = 1;
fileEnd = false;
while ~fileEnd % Read all of the 1250 lines
% FirstLineFormat=’int int int int int int’
% OtherLineFormat=’int char float float float float float float’
try
if k == 1
FieldData_tmp(k,:) = [fread(fileID, 6, ‘int’)’ 0 0 ]; % Read first line
else
FieldData_tmp(k,:) = [fread(fileID, 1, ‘int’) fread(fileID, 1, ‘char’) fread(fileID, 6, ‘float’)’]; % Read other lines
end
catch
fileEnd = true; % No lines left
end
k = k+1;
end
fclose(fileID);
end
end Hello! My code currently opens 120 folders one by one and each folder contains 2 binary files. The code then opens each binary file one by one and reads it. These binary files have 1250 lines each and the lines are read once again one by one.
Is it possible to have the code read all of the lines of 1 binary file at once, not go through 1250 lines sequentially?
The format of the first line is ‘int int int int int int’ (6x int) and the format of all other lines is ‘int char float float float float float float’ (1x int, 1x char, 6x float). The code can be found below.
If there are other suggestions for speeding up the code, please do mention them.
listing = dir(ResultsFolder); % Folder with all 120 folders
for i = 1:length(listing) % Goes from 1 to 120
FieldFolder = [listing(i).folder ” listing(i).name]; % Open 1 of the 120 folders
listingFields = dir(FieldFolder);
FieldFiles = listingFields(contains({listingFields.name},sprintf(‘%A.fld’))); % Get the 2 binary files of interest
for j = 1:length(FieldFiles) % Goes from 1 to 2
fileID = fopen([FieldFolder ” FieldFiles(j).name]); % Open binary file
k = 1;
fileEnd = false;
while ~fileEnd % Read all of the 1250 lines
% FirstLineFormat=’int int int int int int’
% OtherLineFormat=’int char float float float float float float’
try
if k == 1
FieldData_tmp(k,:) = [fread(fileID, 6, ‘int’)’ 0 0 ]; % Read first line
else
FieldData_tmp(k,:) = [fread(fileID, 1, ‘int’) fread(fileID, 1, ‘char’) fread(fileID, 6, ‘float’)’]; % Read other lines
end
catch
fileEnd = true; % No lines left
end
k = k+1;
end
fclose(fileID);
end
end binary MATLAB Answers — New Questions