How to convert a folder of .bin files into .mat files?

With the code below, I was able to read a folder containing .bin files.. in the end it converts all .bin file into a single .mat file which is not so easy to process... Can somebody provide me the code for converting each .bin file into .mat file( without changing the name) to be saved within the folder itself... Please tell me the importance of header files.. Whether i need it during processing steps...
% clear;close all;clc;
binPath = 'MSR Daily Activity 3D dataset\Depth';
for ai = 1:16
for si = 1:10
for ei = 1:2
%%%%%%%%%%%%%
%%%%%%%%%%%%%
[acsr,susr,exsr]=getsr(ai,si,ei);
%%%%%%getsr(ai,si,ei) convert ai,si,ei to double bits
%%%%%%for example, if ai=3, acsr is 03
%%%%%%%%%%%
binfile = [binPath,'\a',acsr,'_s',susr,'_e',exsr,'_depth.bin'];
if ~exist(binfile,'file');
disp('error');
continue;
end;
disp(binfile);
fileread = fopen(binfile);
if fileread<0
disp('no such file.');
return;
end
header = fread(fileread,3,'uint=>uint');
nnof = header(1); ncols = header(2); nrows = header(3);
depths = zeros(ncols, nrows, nnof);
for f = 1:nnof
frame = zeros( ncols, nrows);
for row = 1:nrows
tempRow = fread(fileread, ncols, 'uint=>uint');
tempRowID = fread(fileread, ncols, 'uint8');%%%%%
frame(:,row) = tempRow;
end
depth(:,:,f) = frame;
clear tempRow tempRowID;
end
fclose(fileread);
end
end
end
end

1 Commento

The question is not clear. You tell us, that the data is saved as a MAT file, but the code does not create a MAT file. Therefore we cannot see, what is contained in this file. You ask for "header files" but do not explain, which kind of "header files" are meant. The binary files contain a header, as your code shows. This means that some information for the handling of the data is written at the start of the file. In your case it is the number of rows and columns. But this is not a "header file", but a "file header".

Accedi per commentare.

Risposte (1)

Jan
Jan il 19 Ott 2016
Modificato: Jan il 19 Ott 2016
I guess - an exact answer is not possible yet:
Simply insert this after the fclose(fileread) command:
save([binfile(1:end-4), '.mat'], 'depth');

8 Commenti

Sir, thank you very much for your answer.. The folder contains 320 files in .bin format.. When i ran the code above, i was able to read the files.. and all the .bin files are converted into two .mat files 1. depth (actual) 2. depths(file header), Now i want to know whether we have to use the file header during image processing.
No, you do not have to use the file header during image processing.
Imagine that you have file that contains 1394 readings for a rectangular area. Now, was the rectangle 2 x 697, or was it 34 * 41, or was it 17 * 82 or was it 697 x 2 or was it 41 x 34 or was it 82 x 17? Was the data arranged down the rows or across the columns? Is the first reading the reading for the top left corner, or for the bottom left? You do not know unless it is always the same or else in the file or in a different file there is a record that says (for example) "this is 41 x 34 and the data was stored across the rows from the top left corner". That's what the header file is for, to tell you how to interpret the data.
.bin files are often multispectral files and if you do not have the details about how bands are interleaved then you can get yourself confused easily.
Thanks for your excellent description.. Without such people like you it will be too difficult to do research..
preksha pareek
preksha pareek il 16 Set 2017
Modificato: preksha pareek il 16 Set 2017
In above code it is giving error as undefined function getsr() in [acsr,susr,exsr]=getsr(ai,si,ei);
Is this provided by signal processing toolbox?
No, getsr() is not part of any Mathworks product; it appears to be from the File Exchange contribution that I linked to.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 19 Ott 2016

Commentato:

il 17 Set 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by