0001 function SaveBinary(filename,data,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
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
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);