function savebinary(file,precision,m,wantappend) <file> is a file location <precision> is something like 'int16' <m> is a matrix <wantappend> (optional) is whether to append to <file>. default: 0. write <m> to <file>. for the machine format, we use IEEE floating point with little-endian byte ordering (see fopen). see also loadbinary.m. example: savebinary('test','uint8',repmat(0:255,[2 1])); isequal(loadbinary('test','uint8',[0 256],[255 256]),repmat([254 255],[2 1]))
0001 function savebinary(file,precision,m,wantappend) 0002 0003 % function savebinary(file,precision,m,wantappend) 0004 % 0005 % <file> is a file location 0006 % <precision> is something like 'int16' 0007 % <m> is a matrix 0008 % <wantappend> (optional) is whether to append to <file>. default: 0. 0009 % 0010 % write <m> to <file>. for the machine format, we use IEEE floating 0011 % point with little-endian byte ordering (see fopen). 0012 % 0013 % see also loadbinary.m. 0014 % 0015 % example: 0016 % savebinary('test','uint8',repmat(0:255,[2 1])); 0017 % isequal(loadbinary('test','uint8',[0 256],[255 256]),repmat([254 255],[2 1])) 0018 0019 % constants 0020 machineformat = 'l'; 0021 0022 % input 0023 if ~exist('wantappend','var') || isempty(wantappend) 0024 wantappend = 0; 0025 end 0026 0027 % open file 0028 fid = fopen(file,choose(wantappend,'a','w'),machineformat); 0029 assert(fid ~= -1,'<file> could not be opened for writing'); 0030 0031 % do it 0032 assert(fwrite(fid,m,precision,0,machineformat)==prod(size(m))); 0033 0034 % close file 0035 assert(fclose(fid)==0);