itsave.m File File __pad1__ __pad1__ File File var1 var1 you can redistribute it and or modify it under the terms of the GNU General Public License as published by the Free Software if write to the Free Software Foundation Foundation either version of the either version of the License License either version of the but WITHOUT ANY either version of the but WITHOUT ANY WARRANTY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License for more details You should have received a copy of the GNU General Public License along with this without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License for more details You should have received a copy of the GNU General Public License along with this program program if if not not if write to the Free Software if write to the Free Software Inc Inc if write to the Free Software Franklin if write to the Free Software Franklin St St if write to the Free Software Franklin Fifth if write to the Free Software Franklin Fifth Floor Floor if write to the Free Software Franklin Fifth if write to the Free Software Franklin Fifth Boston Boston end end nargs nargs size(vars,2) for for ai ai 1:nargs if (exist('OCTAVE_VERSION')) % check for octave vname = deblank(argn(ai+1,:)) octave way of getting parameter name matlab way of getting parameter name is_octave is_octave 1 used by function itsizeof to identify octave else used by function itsizeof to identify octave else vname vname inputname(ai+1) used by function itsizeof to identify matlab end used by function itsizeof to identify matlab end v v vars{ai} is_scalar is_scalar all(size(v) == 1) data_bytes data_bytes itsizeof(uint64(0),is_octave) + itsizeof(char(0),is_octave) * prod(size(v)) else a matrix else a matrix hdr_bytes hdr_bytes 3 * itsizeof(uint64(0),is_octave) + size(vname,2)+1 ... + itsizeof('bmat',is_octave)+1 +1 end end block_bytes block_bytes hdr_bytes + data_bytes Write data type end else a matrix Write data type string string Write data type empty description string and data size Write data type empty description string and data size bvec bvec Write data type empty description string and data size Write data type empty description string and data size ivec ivec dvec dvec Write data for Write data for i i 1:size(v(:),1) fwrite(fid, real(v(i)), 'float64') Write data for Write data for j j 1:size(v,2) for i=1:size(v,1) fwrite(fid, real(v(i,j)), 'float64') else nbytes nbytes tmp.bytes end end id id types: (see AUTHORS file for a list of contributors software) types: see AUTHORS file for a list of contributors software either version of the either version of the or (at your option) any later version.%%This program is distributed in the hope that it will be useful or at your option nargin > 1 nargin > 1 varargin () varargin else else error ('Usage:itsave("fname.it", var1,[var2],...)') error 'Usage:itsave("fname.it", var1,[var2],...)' id id file_version () file_version if (fid==-1) error(err_msg) if fid =-1 fid, 'IT++%c', file_version fid, 'IT++%c', file_version fprintf () fprintf true if true if scalar (for future use) is_vector=0 scalar for future use sum (size(v) > 1)< sum size(v) 1 true if a true if a vector (or a scalar) is_intbin=0 vector or a scalar min (min(v==round(v))) min min(v==round(v)) true if integer or binary true if integer or binary if (isreal(v)&&is_intbin)%binary or integer type if(max(max(v)) if isreal(v)&& is_intbin Write header sizes Writes header sizes fwrite (fid,[hdr_bytes data_bytes block_bytes], 'uint64') fwrite fid 'uint64' Write variable name as string Writes variable name as string fprintf (fid, '%s%c', vname) fprintf fid '%s%c' vname fwrite (fid, 0, 'char') fwrite fid 0 'char' Write data type empty description string and data size Write data type empty description string and data size if (is_vector) fprintf(fid if is_vector fwrite (fid, size(v(:), 1), 'uint64') fwrite fid size(v(:), 1) 'uint64' else a matrix else a matrix fprintf (fid, 'bmat') fprintf fid 'bmat' fwrite (fid, size(v), 'uint64') fwrite fid size(v) 'uint64' end Write data end Write data fwrite (fid, v, 'char') fwrite fid v 'char' else a matrix else a matrix fprintf (fid, 'imat') fprintf fid 'imat' end Write data end Write data fwrite (fid, v, 'int32') fwrite fid v 'int32' end binary or integer end binary or integer elseif (isa(v, 'double'))%double precision floating point type if(isreal(v))%Check if real values%Calculate sizes if(is_vector) hdr_bytes elseif isa(v, 'double') else a matrix else a matrix fprintf (fid, 'dmat') fprintf fid 'dmat' end end fwrite (fid, v, 'float64') fwrite fid v 'float64' Write variable name as string Write variable name as string fprintf (fid, '%s', vname) fprintf fid '%s' vname empty description string and data size empty description string and data size fprintf (fid, 'dcvec') fprintf fid 'dcvec' fwrite (fid, imag(v(i)), 'float64') fwrite fid imag(v(i)) 'float64' end else a matrix Write data type empty description string and data size end else a matrix Write data type empty description string and data size fprintf (fid, 'dcmat') fprintf fid 'dcmat' fwrite (fid, imag(v(i, j)), 'float64') fwrite fid imag(v(i, j)) 'float64' end end end end real or complex else end end end end real or complex else warning (['Variable '''vname '''is neither a vector nor matrix.Not saved.']) warning end end end end fclose (fid) fclose fid sizeof sizeof function (like C-style sizeof)%returns no.bytes used by a variable function nbytes function like C-style sizeof %File:itsave.m %Brief:SavesMatlab/OctaveworkspacevariablestoanIT++itfile %Author:TonyOttossonandAdamPiatyszek % %Usage:itsave("fname.it",var1,[var2],...) % %ThisfunctionsavesasetofMatlab/OctaveworkspacevariablestoanIT++ %fileformat.Currently,onlyvectorsand2-Dmatricescanbesaved.The %typeofdatasavedisdetectedautomaticallyandcanbeoneofthe %followingtypes:bvec,bmat,ivec,imat,vec,mat,cvec,cmat. % %------------------------------------------------------------------------- % %IT++-C++libraryofmathematical,signalprocessing,speechprocessing, %andcommunicationsclassesandfunctions % %Copyright(C)1995-2007(seeAUTHORSfileforalistofcontributors) % %Thisprogramisfreesoftware;youcanredistributeitand/ormodify %itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby %theFreeSoftwareFoundation;eitherversion2oftheLicense,or %(atyouroption)anylaterversion. % %Thisprogramisdistributedinthehopethatitwillbeuseful, %butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof %MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe %GNUGeneralPublicLicenseformoredetails. % %YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense %alongwiththisprogram;ifnot,writetotheFreeSoftware %Foundation,Inc.,51FranklinSt,FifthFloor,Boston,MA02110-1301USA % %------------------------------------------------------------------------- functionitsave(fname,varargin) if(nargin>1) vars=varargin; else error('Usage:itsave("fname.it",var1,[var2],...)'); end nargs=size(vars,2); %Thecurrentfile-versionofit_file file_version=3; [fid,err_msg]=fopen(fname,'wb','ieee-le'); if(fid==-1) error(err_msg); end %Writeafileheaderconsistingof"IT++"andacharcontainingthefile %versionnumber fprintf(fid,'IT++%c',file_version); forai=1:nargs if(exist('OCTAVE_VERSION'))%checkforoctave vname=deblank(argn(ai+1,:));%octavewayofgettingparametername is_octave=1;%usedbyfunctionitsizeoftoidentifyoctave else vname=inputname(ai+1);%matlabwayofgettingparametername is_octave=0;%usedbyfunctionitsizeoftoidentifymatlab end v=vars{ai}; is_scalar=all(size(v)==1);%trueifscalar(forfutureuse) is_vector=0;%(sum(size(v)>1)<=1);%trueifavector(orascalar) is_intbin=0;%min(min(v==round(v)));%trueifintegerorbinary if(isreal(v)&&is_intbin)%binaryorintegertype if(max(max(v))==1&&min(min(v))==0)%binarytype %Calculatesizes if(is_vector) hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('bvec',is_octave)+1+1; data_bytes=itsizeof(uint64(0),is_octave)+itsizeof(char(0),is_octave)*prod(size(v)); else%amatrix hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('bmat',is_octave)+1+1; data_bytes=2*itsizeof(uint64(0),is_octave)+itsizeof(char(0),is_octave)*prod(size(v)); end block_bytes=hdr_bytes+data_bytes; %Writeheadersizes fwrite(fid,[hdr_bytesdata_bytesblock_bytes],'uint64'); %Writevariablenameasstring fprintf(fid,'%s%c',vname);fwrite(fid,0,'char'); %Writedatatypestring,emptydescriptionstringanddatasize if(is_vector) fprintf(fid,'bvec');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v(:),1),'uint64'); else%amatrix fprintf(fid,'bmat');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v),'uint64'); end %Writedata fwrite(fid,v,'char'); else%integertype %Calculatesizes if(is_vector) hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('ivec',is_octave)+1+1; data_bytes=itsizeof(uint64(0),is_octave)+itsizeof(int32(0),is_octave)*prod(size(v)); else%amatrix hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('imat',is_octave)+1+1; data_bytes=2*itsizeof(uint64(0),is_octave)+itsizeof(int32(0),is_octave)*prod(size(v)); end block_bytes=hdr_bytes+data_bytes; %Writeheadersizes fwrite(fid,[hdr_bytesdata_bytesblock_bytes],'uint64'); %Writevariablenameasstring fprintf(fid,'%s%c',vname);fwrite(fid,0,'char'); %Writedatatypestring,emptydescriptionstringanddatasize if(is_vector) fprintf(fid,'ivec');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v(:),1),'uint64'); else%amatrix fprintf(fid,'imat');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v),'uint64'); end %Writedata fwrite(fid,v,'int32'); end%binaryorinteger elseif(isa(v,'double'))%doubleprecisionfloatingpointtype if(isreal(v))%Checkifrealvalues %Calculatesizes if(is_vector) hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('dvec',is_octave)+1+1; data_bytes=itsizeof(uint64(0),is_octave)+itsizeof(double(0),is_octave)*prod(size(v)); else%amatrix hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('dmat',is_octave)+1+1; data_bytes=2*itsizeof(uint64(0),is_octave)+itsizeof(double(0),is_octave)... *prod(size(v)); end block_bytes=hdr_bytes+data_bytes; %Writeaheader fwrite(fid,[hdr_bytesdata_bytesblock_bytes],'uint64'); %Writesvariablenameasstring fprintf(fid,'%s%c',vname);fwrite(fid,0,'char'); if(is_vector) fprintf(fid,'dvec');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v(:),1),'uint64'); else%amatrix fprintf(fid,'dmat');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v),'uint64'); end fwrite(fid,v,'float64'); else%complexvalues %Calculatesizes if(is_vector) hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('dcvec',is_octave)+1+1; data_bytes=itsizeof(uint64(0),is_octave)+2*itsizeof(double(0),is_octave)... *prod(size(v)); else%amatrix hdr_bytes=3*itsizeof(uint64(0),is_octave)+size(vname,2)+1... +itsizeof('dcmat',is_octave)+1+1; data_bytes=2*itsizeof(uint64(0),is_octave)+2*itsizeof(double(0),is_octave)... *prod(size(v)); end block_bytes=hdr_bytes+data_bytes; %Writesheadersizes fwrite(fid,[hdr_bytesdata_bytesblock_bytes],'uint64'); %Writevariablenameasstring fprintf(fid,'%s',vname);fwrite(fid,0,'char'); if(is_vector) %Writedatatypestring,emptydescriptionstringanddatasize fprintf(fid,'dcvec');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v(:),1),'uint64'); %Writedata fori=1:size(v(:),1) fwrite(fid,real(v(i)),'float64'); fwrite(fid,imag(v(i)),'float64'); end else%amatrix %Writedatatypestring,emptydescriptionstringanddatasize fprintf(fid,'dcmat');fwrite(fid,0,'char');fwrite(fid,0,'char'); fwrite(fid,size(v),'uint64'); %Writedata forj=1:size(v,2) fori=1:size(v,1) fwrite(fid,real(v(i,j)),'float64'); fwrite(fid,imag(v(i,j)),'float64'); end end end end%realorcomplex else warning(['Variable'''vname'''isneitheravectornormatrix.Notsaved.']); end end fclose(fid); %sizeoffunction(likeC-stylesizeof) %returnsno.bytesusedbyavariable functionnbytes=itsizeof(in,is_octave) if(~is_octave) tmp=whos('in'); nbytes=tmp.bytes; %matlabuses2bytesbydefaultforchar %overwriteusing1byteforfileformat if(ischar(in)) nbytes=size(in,2); end; else nbytes=sizeof(in); end;