function save_binary( filename, matrix, row_labels, col_headers, name ) % save_binary( FILENAME, MATRIX, ROW_LABELS, COL_HEADERS, NAME ) % % Saves data matrix in a binary format. % % FILENAME: String containing the filename to write. % MATRIX: n-by-m data matrix. % ROW_LABELS: n-by-1 cell array containing row labels. % COL_HEADERS: m-by-1 cell array containing column headers. % NAME: Description string (ie. the upper-left element in data text-files). % % WARNING: No error checking is performed for clarity. % Opens the file for writting in the desired format. machineformat = ['ieee-le']; fid = fopen(filename, 'w', machineformat, 'ISO-8859-15'); % Size of data matrix. [ n m ] = size(matrix); % Number of rows: a 32-bits signed integer. fwrite( fid, n, 'int32', machineformat ); % Number of columns: a 32-bits signed integer. fwrite( fid, m, 'int32', machineformat ); % Data matrix: n-by-m 64-bits floating-point values. % NOTE: data matrix is transposed before calling fwrite() % in order to save it in row-major format. fwrite( fid, matrix', 'real*8', machineformat ); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Optionally, saves row labels, column headers, and name (as plain-text) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Closes file and returns if there is neither labels nor name. if ( (length(col_headers) == 0) && (length(row_labels) == 0) && (length(name) == 0) ) fclose(fid); return; end; % Row labels (tab-separated). if ( length(row_labels) > 0 ) len = length( row_labels{1} ); fprintf(fid,'%s',row_labels{1}); for i=2:n len = length( row_labels{i} ); fprintf(fid,'\t%s',row_labels{i}); end end % A newline character. fprintf(fid,'\n'); % Column headers (tab-separated). if ( length(col_headers) > 0 ) len = length( col_headers{1} ); fprintf(fid,'%s',col_headers{1}; for i=2:m len = length( col_headers{i} ); fprintf(fid,'\t%s',col_headers{i}; end end % A newline character. fprintf(fid,'\n'); % Name: description string (upper-left field in data text files) if ( length(name) > 0 ) fprintf(fid,'%s\n',name); end fclose(fid);