% SAVE_IT Saves an IT++ file to disk. % % SAVE_IT fname.it saves all workspace variables to the IT++ file 'fname.it'. % SAVE_IT fname.it x y z saves variables x, y, and z to the IT++ file 'fname.it'. % % save_it.m,v 1.1.1.1 2001/10/10 13:31:08 tonyottosson Exp % % See also LOAD_IT function save_it(fname, varargin) if (nargin > 1) vars = varargin; else vars = evalin('caller', 'who(''*'')')'; end endianity = 1; % Always big endian for now... file_version = 1; % The current file-version of it_file if (endianity == 0) [fid, err_msg] = fopen(fname, 'wb', 'ieee-le'); else [fid, err_msg] = fopen(fname, 'wb', 'ieee-be'); end if (fid == -1) error(err_msg); end % Writes a file header consisting of "IT++" and an char containing the file version number fprintf(fid, 'IT++%c', file_version); % Should a vector with only one element be saved as a scalar in IT++ or as a vector? for vname=vars vname = char(vname); v = evalin('caller', vname); if (isa(v, 'double')) % double precision floating point type is_scalar = (size(v,1)==1 & size(v,2)==1); % true if scalar (for future use) is_vector = (size(v,1)==1 | size(v,2)==1); % true if a vector (or a scalar) % Writes a char telling what binary format is used fwrite(fid, [endianity], 'char'); if (isreal(v)) % Check if real values hdr_bytes = 1 + 3 * 4 + size(vname,2)+1 + 5; if (is_vector) data_bytes = 1 * 4 + 8 * prod(size(v)); else % a matrix data_bytes = 2 * 4 + 8 * prod(size(v)); end block_bytes = hdr_bytes + data_bytes; % Writes a header fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint32'); % Writes variable name as string fprintf(fid, '%s%c', vname, 0); if (is_vector) fprintf(fid, 'dvec%c', 0); fwrite(fid, size(v(:),1), 'uint32'); else % a matrix fprintf(fid, 'dmat%c', 0); fwrite(fid, size(v), 'uint32'); end fwrite(fid, v, 'float64'); else % complex values hdr_bytes = 1 + 3 * 4 + size(vname,2)+1 + 6; % 3*4 is for sized, 6 is for type (dcvec) if (is_vector) data_bytes = 1 * 4 + 2 * 8 * prod(size(v)); else % a matrix data_bytes = 2 * 4 + 2 * 8 * prod(size(v)); end block_bytes = hdr_bytes + data_bytes; % Writes a header fwrite(fid, [hdr_bytes data_bytes block_bytes], 'uint32'); % Writes variable name as string fprintf(fid, '%s%c', vname, 0); if (is_vector) fprintf(fid, 'dcvec%c', 0); fwrite(fid, size(v(:),1), 'uint32'); for i=1:size(v(:),1) fwrite(fid, real(v(i)), 'float64'); fwrite(fid, imag(v(i)), 'float64'); end else % a matrix fprintf(fid, 'dcmat%c', 0); fwrite(fid, size(v), 'uint32'); for j=1:size(v,2) for i=1:size(v,1) fwrite(fid, real(v(i,j)), 'float64'); fwrite(fid, imag(v(i,j)), 'float64'); end end end end % real or complex else warning(['Variable ''' vname ''' is not a vector or matrix. Not saved.']); end end