Home > FMAToolbox > IO > SaveBinary.m

SaveBinary

PURPOSE ^

SaveBinary - Save data in a multiplexed binary file.

SYNOPSIS ^

function SaveBinary(filename,data,varargin)

DESCRIPTION ^

SaveBinary - Save data in a multiplexed binary file.

  USAGE

    SaveBinary(filename,data,<options>)

    filename       file to save to
    data           data to save
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'precision'   sample precision (default = 'int16')
     'mode'        either 'new' to create a new file (default) or 'append'
                   to add to an existing file
    =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function SaveBinary(filename,data,varargin)
0002 
0003 %SaveBinary - Save data in a multiplexed binary file.
0004 %
0005 %  USAGE
0006 %
0007 %    SaveBinary(filename,data,<options>)
0008 %
0009 %    filename       file to save to
0010 %    data           data to save
0011 %    <options>      optional list of property-value pairs (see table below)
0012 %
0013 %    =========================================================================
0014 %     Properties    Values
0015 %    -------------------------------------------------------------------------
0016 %     'precision'   sample precision (default = 'int16')
0017 %     'mode'        either 'new' to create a new file (default) or 'append'
0018 %                   to add to an existing file
0019 %    =========================================================================
0020 
0021 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0022 %
0023 % This program is free software; you can redistribute it and/or modify
0024 % it under the terms of the GNU General Public License as published by
0025 % the Free Software Foundation; either version 3 of the License, or
0026 % (at your option) any later version.
0027 
0028 % Default values
0029 precision = 'int16';
0030 mode = 'new';
0031 
0032 if nargin < 2 | mod(length(varargin),2) ~= 0,
0033   error('Incorrect number of parameters (type ''help <a href="matlab:help SaveBinary">SaveBinary</a>'' for details).');
0034 end
0035 
0036 % Parse options
0037 for i = 1:2:length(varargin),
0038   if ~ischar(varargin{i}),
0039     error(['Parameter ' num2str(i+3) ' is not a property (type ''help <a href="matlab:help SaveBinary">SaveBinary</a>'' for details).']);
0040   end
0041   switch(lower(varargin{i})),
0042     case 'precision',
0043       precision = varargin{i+1};
0044       if ~isastring(precision),
0045         error('Incorrect value for property ''precision'' (type ''help <a href="matlab:help SaveBinary">SaveBinary</a>'' for details).');
0046       end
0047     case 'mode',
0048       mode = lower(varargin{i+1});
0049       if ~isastring(mode,'new','append'),
0050         error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help SaveBinary">SaveBinary</a>'' for details).');
0051       end
0052     otherwise,
0053       error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help SaveBinary">SaveBinary</a>'' for details).']);
0054   end
0055 end
0056 
0057 sizeInBytes = 0;
0058 switch precision,
0059     case {'uchar','unsigned char','schar','signed char','int8','integer*1','uint8','integer*1'},
0060         sizeInBytes = 1;
0061     case {'int16','integer*2','uint16','integer*2'},
0062         sizeInBytes = 2;
0063     case {'int32','integer*4','uint32','integer*4','single','real*4','float32','real*4'},
0064         sizeInBytes = 4;
0065     case {'int64','integer*8','uint64','integer*8','double','real*8','float64','real*8'},
0066         sizeInBytes = 8;
0067 end
0068 
0069 if strcmp(mode,'new'),
0070     f = fopen(filename,'w');
0071     status = fseek(f,0,'bof');
0072 else
0073     f = fopen(filename,'a');
0074     status = fseek(f,0,'eof');
0075 end
0076 if status ~= 0,
0077     fclose(f);
0078     error('Could not start writing (possible reasons include insufficient access permissions).');
0079 end
0080 
0081 fwrite(f,data',precision);
0082 
0083 fclose(f);

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005