1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
3 // Copyright (C) ???? - INRIA - Serge STEER
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11 function savematfile(varargin)
12 // Save variables in a Matlab binary or ASCII file into Scilab
13 // This function has been developped following the 'MAT-File Format' description:
14 // www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf
17 // Verify that all inputs are character strings
18 for k=1:size(varargin)
19 if type(varargin(k))<>10 then
20 error(gettext("All inputs must be character strings."));
26 mtlb_opts=[]; // Options for ASCII format
27 mtlb_thefile=[]; // Name of file to write
28 mtlb_names=[]; // Variable names to save
29 version=[]; // MAT-file version: 4 or 6 or 7 (7.3 not yet implemented)
30 bin=[]; // %T is binary file %F if ASCII file
32 // Default format is binary
37 // Sort all inputs (Options/Names/Filename)
39 while k<=lstsize(varargin)
40 // All options are converted to lower case
41 if part(varargin(k),1)=="-" then
42 varargin(k)=convstr(varargin(k));
47 warning(msprintf(gettext("Option %s not implemented: IGNORED."),"-append"));
53 mtlb_opts=[mtlb_opts varargin(k)];
60 // Verify if one or more field name is/are given
61 if k<=lstsize(varargin) & part(varargin(k),1)<>"-" & mtlb_thefile<>"" then // struct field
62 while k<=lstsize(varargin) & part(varargin(k),1)<>"-"
63 // Add field to variable names
64 mtlb_names=[mtlb_names;varargin(k)];
65 execstr(varargin(k)+"="+stname+"(mtlb_names($))");
68 else // All vars(1)=[];fields have to be saved
69 fields=getfield(1,evstr(stname));
72 mtlb_names=[mtlb_names;kk];
73 execstr(kk+"="+stname+"(mtlb_names($))");
94 mtlb_opts=[mtlb_opts varargin(k)];
98 mtlb_opts=[mtlb_opts varargin(k)];
101 warning(msprintf(gettext("Option %s not implemented: IGNORED."),"-regexp"));
102 while k<=lstsize(varargin) & and(varargin(k)<>["-mat","-ascii"])
106 if isempty(mtlb_thefile) then // Filename
107 mtlb_thefile=pathconvert(varargin(k),%f,%t);
108 if fileparts(mtlb_thefile,"extension")==".mat" & isempty(bin) then // extension .mat and bin not already fixed by options
111 else // Variable names
112 mtlb_names=[mtlb_names;varargin(k)]
118 // Default version 7 for binary files
119 if isempty(version) & bin then
121 warning(gettext("Option -v7 added."));
124 // If no name given then all workspace saved
125 if isempty(mtlb_names) then
128 // Part to delete Scilab variables from mtlb_names (should be improved)
129 mtlb_names(1)=[];// remove varargin
130 mtlb_names(mtlb_names=='savematfile')=[];
131 mtlb_names(($-predef()+1):$)=[]; // clear predefined variables
134 // If binary format and no extension for filename, .mat is added
135 if bin & isempty(strindex(mtlb_thefile,".")) then
136 mtlb_thefile=mtlb_thefile+".mat"
139 // Do not handle function redefinition
144 // LEVEL 4 MAT-file (This part comes from mtlb_save.sci)
146 // Matlab 5 types are not saved (structs...)
147 for k=size(mtlb_names,"*"):-1:1
148 execstr("x="+mtlb_names(k))
149 if and(type(x)<>[1 4 5 6 10]) then
150 warning(msprintf(gettext("Variable %s can not be saved in level 4 MAT-file: IGNORED."),mtlb_names(k)));
155 // Open file for writing
156 [mtlb_fd,err]=mopen(mtlb_thefile,"wb",0)
158 // Clear variable wich are no more used to avoid name conflicts
159 for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
160 if or(mtlb_names==k) then
161 error(msprintf(gettext("Name conflict: it is not possible to save variable with name %s."),k));
164 clear("x","k","rhs","lhs","kk","err","bin","version","mtlb_thefile","mtlb_opts");
166 // Following 'for loop' from SS
167 for mtlb_k=1:size(mtlb_names,"*")
168 // perform changes on variables
169 execstr("x="+mtlb_names(mtlb_k))
175 if norm(imag(x),1)<>0 then it=1,end
181 if norm(imag(x),1)<>0 then it1=1,else it1=0,end
184 // We transpose the sparse matrix so as to ease the conversion to the matlab sparse format
189 x=[x real(v) imag(v);[mn 0 0]]
209 x1=part(x(:),1:max(length(x)))
217 error(gettext("Attempt to write an unsupported data type to an ASCII file."));
222 M = 0 //little endian
227 head=[MOPT*[1000;100;10;1] m,n,it,length(mtlb_names(mtlb_k))+1]
229 head=mput(head,"uil",mtlb_fd);
230 mput([ascii(mtlb_names(mtlb_k)) 0],"c",mtlb_fd);
247 mput(real(x(:).'),flag,mtlb_fd);
249 mput(imag(x(:).'),flag,mtlb_fd);
253 v=mput(x(:).',flag,mtlb_fd);
254 elseif T==2 then //sparse
255 mput(x(:).',flag,mtlb_fd);
259 // End of loop written by SS
261 elseif version==6 then
262 // Open file for writing
263 mtlb_fd=matfile_open(mtlb_thefile, "w");
264 if mtlb_fd == -1 then
265 error(msprintf(gettext("%s: Could not open file ''%s''.\n"),"savematfile",mtlb_thefile))
267 // Clear variable wich are no more used to avoid name conflicts
268 for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
269 if or(mtlb_names==k) then
270 error(msprintf(gettext("Name conflict: it is not possible to save variable with name %s."),k))
273 clear("x","k","rhs","lhs","kk","err","sep","bin","version","mtlb_thefile","mtlb_opts");
275 // Write variables as miMATRIX data type
276 for k=1:size(mtlb_names,"*")
277 %var=evstr(mtlb_names(k));
278 // We transpose the sparse matrix so as to ease the conversion to the matlab sparse format
279 if type(%var)==5 then %var = %var'; end
280 if and(type(%var)<>[9 11 13]) then
281 if ~matfile_varwrite(mtlb_fd, mtlb_names(k), %var, %F) then
282 error(msprintf(gettext("savematfile: could not save variable named %s.\n"), mtlb_names(k)));
285 error(msprintf(gettext("savematfile: could not save variable named %s.\n"), mtlb_names(k)));
289 matfile_close(mtlb_fd);
291 // Open file for writing
292 mtlb_fd=matfile_open(mtlb_thefile, "w");
293 if mtlb_fd == -1 then
294 error(msprintf(gettext("%s: Could not open file ''%s''.\n"),"savematfile",mtlb_thefile))
296 // Clear variable wich are no more used to avoid name conflicts
297 for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
298 if or(mtlb_names==k) then
299 error(msprintf(gettext("Name conflict: it is not possible to save variable with name %s."),k))
302 clear("x","k","rhs","lhs","kk","err","sep","bin","version","mtlb_thefile","mtlb_opts");
304 // Write variables as miCOMPRESSED data type
305 for k=1:size(mtlb_names,"*")
306 %var=evstr(mtlb_names(k));
307 // We transpose the sparse matrix so as to ease the conversion to the matlab sparse format
308 if type(%var)==5 then %var = %var'; end
309 if and(type(%var)<>[9 11 13]) then
310 if ~matfile_varwrite(mtlb_fd, mtlb_names(k), %var, %T) then
311 error(msprintf(gettext("savematfile: could not save variable named %s.\n"), mtlb_names(k)));
314 error(msprintf(gettext("savematfile: could not save variable named %s.\n"), mtlb_names(k)));
318 matfile_close(mtlb_fd);
319 else // Version 7.3 ???
320 error(msprintf(gettext("Version %d MAT-file not implemented."),version));
325 // The end of this function has been adapted from mtlb_save.sci
327 // Matlab 5 types are not saved (structs...)
328 for k=size(mtlb_names,"*"):-1:1
329 execstr("x="+mtlb_names(k))
330 if and(type(x)<>[1 4 5 6 10]) then
331 warning(msprintf(gettext("Variable %s can not be saved in ASCII file: IGNORED."),mtlb_names(k)));
335 if ( (mtlb_opts <> []) & (strindex("-tabs",mtlb_opts)<>[]) ) then
340 if size(mtlb_opts,"*")==1 then //8 digits save
341 mtlb_fmt="(2x,1pe14.7"+sep+")"
343 mtlb_fmt="(2x,1pe23.15"+sep+")"
346 mtlb_fd=file("open",mtlb_thefile,"unknown")
348 // Clear variable wich are no more used to avoid name conflicts
349 for k=["varargin","mtlb_names","mtlb_fmt","mtlb_fd"]
350 if or(mtlb_names==k) then
351 error(msprintf(gettext("Name conflict: it is not possible to save variable with name %s."),k));
354 clear("x","k","rhs","lhs","kk","err","sep","bin","version","mtlb_thefile","mtlb_opts");
356 for mtlb_k=1:size(mtlb_names,"*")
357 // perform changes on variables
358 execstr("x="+mtlb_names(mtlb_k))
361 write(mtlb_fd,real(x),"("+string(size(x,2))+mtlb_fmt+")")
363 write(mtlb_fd,bool2s(x),"("+string(size(x,2))+mtlb_fmt+")")
365 // We need to transpose to conform to the matlab sparse format
366 [ij,x]=spget(real(x'));x=[ij x];
367 write(mtlb_fd,real(x),"(2f8.0,1x"+string(size(x,2))+mtlb_fmt+")")
369 [ij,x]=spget(bool2s(x));x=[ij x];
370 write(mtlb_fd,real(x),"(2f8.0,1x"+string(size(x,2))+mtlb_fmt+")")
372 x=part(x(:),1:max(length(x)))
377 write(mtlb_fd,x1,"("+string(size(x1,2))+mtlb_fmt+")")
380 file("close",mtlb_fd)