%WRITEBINARY Sample file how to write binary data to file. % %See also % % http://www.mathworks.com/support/tech-notes/1400/1403.html % http://www.mathworks.com/support/tech-notes/1600/1602.html % % Thorsten Hansen 2008-05-14 function writebinary(filename) if nargin == 0 % default filename if no argument is given filename = 'square_mat.bin'; end % open the file returns a file handle fid fid = fopen(filename, 'wb'); % check status and report error if fid == -1 error(['Cannot open file ' filename '.']) end % write the header fwrite(fid, 'This is a square matrix', 'char'); % write data to file, delimited by tabs \t fwrite(fid, [1 2 3; 4 5 6; 7 8 9]', 'int32'); % close file using file handle st = fclose(fid); % check status and report error if fid == -1 error(['Cannot close file ' filename '.']) end