x=testmatrix('magic',4);/// \sleftarrow{\normalfont a \verb+4x4+ test matrix } fd=mopen('save.dat','wb'); /// \sleftarrow {\begin{minipage}[t]{5cm}{\nf{}opens a file {for} writing in binary \\ mode (\verb+'b'+ is useful on windows) }\end{minipage}} mput(length('x'),'i',fd) /// \sleftarrow{\normalfont writes an integer. The length of the string \verb+'x'+} mputstr('x',fd) ; /// \sleftarrow{\normalfont writes a string \verb+'x'+} mput(size(x,'r'),'i',fd) /// \sleftarrow{\normalfont writes an integer (number of rows)} mput(size(x,'c'),'i',fd) /// \sleftarrow{\normalfont writes an integer (number of columns)} mput(x,'d',fd); /// \sleftarrow{\normalfont writes an array of doubles } mclose(fd); /// \sleftarrow{\normalfont closes the file output stream} clear x; fd=mopen('save.dat','rb'); /// \sleftarrow{\normalfont opens a file {for} reading in binary mode} ls=mget(1,'i',fd); /// \sleftarrow{\normalfont reads an integer } name=mgetstr(ls,fd); /// \sleftarrow{\normalfont reads a string. The string length is given by \verb+ls+} m=mget(1,'i',fd);n=mget(1,'i',fd); /// \sleftarrow{\normalfont reads two integers \verb+m+ and \verb+n+} data=mget(m*n,'d',fd);/// \sleftarrow{\normalfont reads \verb+m*n+ doubles in a vector } code= msprintf('%s=matrix(data,%d,%d);',name,m,n); execstr(code); /// \sleftarrow{\normalfont reshape the matrix and give it the correct name (i.e \verb+x+)} mclose(fd) ; /// \sleftarrow{\normalfont close the file } and(x == testmatrix('magic',4)) /// \sleftarrow{\normalfont test }