File Exchange

image thumbnail

BIN2MAT

version 1.0.0.0 (2.31 MB) by Eric Ogier
Binary to MAT-file converter

12 Downloads

Updated 23 Apr 2018

View License

BIN2MAT is a GUI application dedicated to the conversion of binary files to MAT v6 file format.
The application performs its task :
- From : raw data binary file (any recursive data structure)
- To : MAT v6 formatted files (MATLAB compliance)
- Through : data structure descriptive dictionary (INI file)
The application includes an internal script interpreter allowing simple dynamic operations and can be used as a standalone executable.

Video demonstration:
https://www.youtube.com/watch?v=hbFnuNFRGcQ&list=PLJXyTqQS4FL3QoWdlGj0WtT7nSYKWqBxv&index=38&t=0s&ab_channel=Pseudonymeoriginal



User manual:
http://fr.slideshare.net/EricOgier/bin2mat-user-manual-59371085

Cite As

Eric Ogier (2021). BIN2MAT (https://www.mathworks.com/matlabcentral/fileexchange/55763-bin2mat), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Eric Ogier

Hello,
The way this application works does not allow you to save data in the same file. Indeed, every time a buffer is read, the data are directly written in the corresponding files, without temporary storage. If you want to write them in an unique MAT-file as soon as they are read, you need to know in advance the size of the data you will read or you will have to write them in a specific size files (needing segmentation or zero padding). You can find more information on the way to write structures in MAT-file standard specification.
For me, the choice I did was more interesting because it allows the user to define the segmentation and to load in MATLAB the data he wants among all the ones extrated.

Rathnakar Shetty P

Dear Sir,
It is very helpful and thank you for your efforts.
Can we save all the filed variables from One Bin file in a single .Mat file using this program? I see it is saved in a Folder as mat files rather than a Mat file with Variables/Fields.
Thank you

Dany

Felix

Eric Ogier

Eric Ogier

Warwick,
I think your problem just comes from the fact the initialization file ”Dictionary_test.ini” is not present in the good directory.

In the script I gave you, you can try to adapt the link to the “dictionary”
Line 62 :
Object.set('Dictionary',fullfile(cd,'Dictionary_test.ini'));
Maybe your dictionary is in not in fullfile(cd,'Dictionary_test.ini')).

If your problem persists, you can try to use directly the core of the application through the “BIN2MAT_class” object by using the following script :

%----------------------------

B2M = BIN2MAT_class();

% Dictionary
Dictionary.Header = [84 69 83 84]; %'0x54455354' i.e 'TEST'
Dictionary.Endianess = 'LittleEndian';
Dictionary.Variables = {'Sample','Angle','Sinus','Cosinus','Phase','Quadrature'};
Dictionary.Types = {'uint32','single','single','single','int8','int8'};

set(B2M,...
'Source',fullfile(cd,'BIN2MAT_BIN\Test.bin'),...
'Dictionary',Dictionary,...
'Destination',fullfile(cd,'BIN2MAT_MAT'));
B2M.convert();
B2M.delete();

%----------------------------

BIN2MAT is just a GUI calling this object and allowing to read dictionaries through an INI file reader without defining the structure Dictionary.Header/Endianess/Variables/etc required by "BIN2MAT_class".

I hope it will work better than time.

Warwick

Eric,
Thank you for your effort in investigating and providing a potential solution. Unfortunately it feels that it is far too sophisticated for me to get to work. But perhaps one more try?
Using your suggested script, I get this error --
Error using feof
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in INI/read (line 94)
while ~feof(ID).

Eric Ogier

Warwick,

I think I found the problem you met using a configuration script. The fact you use a MAC should only cause a problem with the command “winopen”, I used several times.

The file titled “BIN2MAT_script.m” is a “BIN2MAT” script (by opposition to a true MATLAB script), supposed to be used inside the main GUI only, through the internal interpreter (menu “Automation interface”). This way to work is useful if you use BIN2MAT as an executable. Though the internal interpreter has limitations (based on “eval” function), a lot of simple operations can be executed. I reused this interpreter in the program called “AVAR” also available in File Exchange.

If you prefer to use BIN2MAT calling it from MATLAB, I suggest the following example adapted from “BIN2MAT_script.m”. I just add the line “Object = BIN2MAT_GUI();” (line 47),replaced “BIN2MAT_GUI.[…]” by “Object.[…]” (the internal interpreter allows you not to write “Object = BIN2MAT_GUI();” and directly use BIN2MAT_GUI.set/get/start) and adapted the line 62 (“Object.set('Dictionary',fullfile(cd,'Dictionary_test.ini'));”, the good dictionary for “Test.bin”). Then, if it works well, this script should make you choose a directory where you put all your binary sources (e.g. make several copies of the example “Test.bin”) and automatically save the corresponding data in MAT-files in specific subfolders.

%--- A script to save in the main folder ---

% Selection of the source directory
Directory = uigetdir(cd,'SCRIPT : select a directory containing sources files (*.ini)');
if isempty(Directory)
return
end

% Exploration of the selected directory (*.bin files)
List = {};
Folders = {Directory};
Extensions = {'.bin'};

while ~isempty(Folders)

% Elements in folder
Folder = Folders{1};
Data = dir(fullfile(Folder,'*.*'));
b = [Data(:).isdir];
Data = {Data(:).name};

% Files
Files = Data(~b);
for File = Files
[~,~,e] = fileparts(File{1});
if ismember(e,Extensions) || strcmp(Extensions{1},'.*')
List(numel(List)+1,1) = fullfile(Folder,File); %#ok<SAGROW>
end
end

% Subfolders
Subfolders = Data(b);
Subfolders = cellfun(@(f)fullfile(Folder,f),Subfolders(3:numel(Subfolders)),'UniformOutput',false);
Folders = [Folders{2:numel(Folders)};Subfolders];

end

N = numel(List);
if N == 0
msgbox(sprintf('No file in directory "%s".',Directory),'Error');
end

% Waitbar
if N > 1
WB = waitbar(0,'','Name','Progression','CreateCancelBtn','setappdata(gcbf,''canceling'',1)');
setappdata(WB,'canceling',0);
end

Object = BIN2MAT_GUI();

% Conversion
for n = 1:N

% Source
Source = List{n};
Object.set('Source',Source);

% Destination
[Directory,File,~] = fileparts(Source);
Destination = fullfile(Directory,File);
Object.set('Destination',Destination);

% Dictionary
Object.set('Dictionary',fullfile(cd,'Dictionary_test.ini'));

% Options
Object.set('Segmentation','No segmentation');
Object.set('Open','No');
Object.set('Plot','No');

% Waitbar
if N > 1
File2 = strrep(File,'_',[char(92) '_']);
waitbar((n-1)/N,WB,sprintf('Converting file "%s" (%u/%u)',File2,n,N));
if getappdata(WB,'canceling')
break
end
end

% Start
Object.start();

% Waiting for an interruption
if Object.get('Interruption')
break
end

end

% Waitbar
if N > 0
delete(WB);
end

Warwick

I am probably missing something obvious and/or doing something wrong, but I can't get any of the scripts to work.
I'm using Version 2015b on a Mac so that could be the problem.
One error message I get frequently is ... "The class BIN2MAT_GUI has no Constant property or Static method named 'set'.
Error in BIN2MAT_script (line 52)
BIN2MAT_GUI.set('Source',Source);"

I have put the Test.bin file into my working directory. Could you give a simple example of how to use any of BIN2MAT, Bin2MAT_GUI, and BIN2MAT_script to even access Test.bin?
thanks either way.

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!