From bug-request at octave dot org Wed Jun 1 12:48:38 2005 Subject: Re: general load/save (in)compatibility From: Ben Barrowes To: bug at octave dot org CC: David dot Bateman at motorola dot com Date: Wed, 01 Jun 2005 11:47:30 -0600 This is a multi-part message in MIME format. --------------070406020607010303070504 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Here is a proposed small patch that appends a ".mat" onto the filename to be save under the following conditions: 1 - the save filename has no file extension 2 - no filetype switch is given 3 - default_save_options points to a matfile type (those filetypes which get mapped to LS_MAT_BINARY, LS_MAT5_BINARY, or LS_MAT7_BINARY in load-save.h) For example: octave:1> aa=magic(4) aa = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 octave:2> save zz1 aa octave:3> save -text zz1 aa octave:4> save zz1.dat aa octave:5> default_save_options default_save_options = -V6 octave:6> ".mat' is appended to the filename only in the case of command 2, and only because default_save_options=-V6 is a matfile type. If default_save_options=-text or some other filetype, no ".mat" would have been appended. Here is a ChangeLog entry: 2005-06-01 Ben Barrowes * load-save.cc: Save now appends ".mat" under the following circumstances: 1 - the save filename has no file extension 2 - no filetype switch is given 3 - default_save_options points to a matfile type (those filetypes which get mapped to LS_MAT_BINARY, LS_MAT5_BINARY, or LS_MAT7_BINARY in load-save.h) *** ../octave-2.9.3/src/load-save.cc 2005-05-18 06:20:53.000000000 -0600 --- load-save.cc 2005-06-01 11:15:53.000000000 -0600 *************** the file at file{data} in Octave's binary *** 1629,1634 **** --- 1629,1643 ---- else { std::string fname = file_ops::tilde_expand (argv[i]); + + if (user_file_format == LS_UNKNOWN + && (format == LS_MAT_BINARY + || format == LS_MAT5_BINARY + || format == LS_MAT7_BINARY) + && fname.find(".",0) == std::string::npos) + { + fname.append(".mat"); + } i++; Ben David Bateman wrote: > Ben Barrowes wrote: > >>>> - matlab appends a ".mat" onto saved files if there is no >>>> extension on the save filename and no filetype switch is given. >>>> Fix=> make octave add ".mat" in cases where the save filename has >>>> no file extension and no filetype switch is given *and* if >>>> default_save_options points to a matfile type. This extra test is >>>> required because matlab currently has no way to default to saving >>>> as ascii files. >>> >>> >>> >>> >>> Third option the user explictly adds the ".mat" extension when they >>> want it... Ok, it probably makes sense to add ".mat" to filenames >>> when we are talking about matlab formats. Supply a patch and I'm >>> sure JWE would accept it, though not if it does it for all file types. >>> >> What file would I patch... load-save.cc ? > > > Yes it would be in this file that the patch is needed. The problem is > that you'd have to strip the filename of any extension, see if it is > the same as the original filename and then add the ".mat" only if a > matlab file format is used.. > >> >>>> - octave's -text and -ascii (are these different?) save in a >>>> format incompatible with matlab. I think it would be compatible if >>>> the # comment character in octave's save format was changed to %. >>>> Then both matlab and octave could load the file. >>> >>> >>> >>> >>> -ascii is on its way out and being replaced by "-text" exactly for >>> this reason. Wait till 3.0 and this will be address. Till then use >>> -mat-ascii... >>> >> I don't see the -mat-ascii switch available in the load/save helps. >> Also, it doesn't seem to be recognized in 2.9.3: >> octave:13> save -mat-ascii test1 aa >> warning: save: no such variable `test1' >> octave:14> >> >> will this be in 3.0? >> > Err, true... I was working from memory think that this had already > been implemented. It hasn't yet, and you can only read matlab ascii > files... So when the meaning of "-ascii" changes you'll get the matlab > ascii file writing support. You could probably get something similar > using fprintf till then if it is a strict requirement. > > >>> >>> >>>> octave:59> a1=load('test2') >>>> error: value on right hand side of assignment is undefined >>>> error: evaluating assignment expression near line 59, column 3 >>>> octave:59> >>>> fails. >>> >>> >>> >>> >>> See Dmitri's response... >>> >>> D. >>> >> I read Dmitri's response, but he only reported that the above worked >> in 2.1.71. Have you tried to load a file like test2 in 2.9.3? > > > > Just did... It worked fine with me... Are you sure there wasn't a > hidden character in your file? To be sure I include as an attachement > the version of the test file I used... > > regards > David > >------------------------------------------------------------------------ > > 8.0000000e+00 1.0000000e+00 6.0000000e+00 > 3.0000000e+00 5.0000000e+00 7.0000000e+00 > 4.0000000e+00 9.0000000e+00 2.0000000e+00 > > --------------070406020607010303070504 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Here is a proposed small patch that appends a ".mat" onto the filename to be save under the following conditions:
1 - the save filename has no file extension
2 - no filetype switch is given
3 - default_save_options points to a matfile type (those filetypes which get mapped to LS_MAT_BINARY, LS_MAT5_BINARY, or LS_MAT7_BINARY in load-save.h)


For example:
octave:1> aa=magic(4)
aa =

  16   2   3  13
   5  11  10   8
   9   7   6  12
   4  14  15   1

octave:2> save zz1 aa
octave:3> save -text zz1 aa                                                   
octave:4> save zz1.dat aa
octave:5> default_save_options
default_save_options = -V6
octave:6>

".mat' is appended to the filename only in the case of command 2, and only because default_save_options=-V6 is a matfile type. If default_save_options=-text or some other filetype, no ".mat" would have been appended.



Here is a ChangeLog entry:

2005-06-01  Ben Barrowes  <barrowes@alum dot mit dot edu>

    * load-save.cc: Save now appends ".mat"
        under the following circumstances:
      1 - the save filename has no file extension
      2 - no filetype switch is given
      3 - default_save_options points to a matfile type
          (those filetypes which get mapped to
          LS_MAT_BINARY, LS_MAT5_BINARY, or LS_MAT7_BINARY
          in load-save.h)



*** ../octave-2.9.3/src/load-save.cc    2005-05-18 06:20:53.000000000 -0600
--- load-save.cc        2005-06-01 11:15:53.000000000 -0600
*************** the file at file{data} in Octave's binary
*** 1629,1634 ****
--- 1629,1643 ----
    else
      {
        std::string fname = file_ops::tilde_expand (argv[i]);
+      
+       if (user_file_format == LS_UNKNOWN
+           && (format == LS_MAT_BINARY
+               || format == LS_MAT5_BINARY
+               || format == LS_MAT7_BINARY)
+           && fname.find(".",0) == std::string::npos)
+         {
+           fname.append(".mat");
+         }
 
        i++;
 


Ben


David Bateman wrote:
Ben Barrowes wrote:

   - matlab appends a ".mat" onto saved files if there is no extension on the save filename and no filetype switch is given. Fix=> make octave add ".mat" in cases where the save filename has no file extension and no filetype switch is given *and* if default_save_options points to a matfile type. This extra test is required because matlab currently has no way to default to saving as ascii files.



Third option the user explictly adds the ".mat" extension when they want it... Ok, it probably makes sense to add ".mat" to filenames when we are talking about matlab formats. Supply a patch and I'm sure JWE would accept it, though not if it does it for all file types.

What file would I patch... load-save.cc ?

Yes it would be in this file that the patch is needed. The problem is that you'd have to strip the filename of any extension, see if it is the same as the original filename and then add the ".mat" only if a matlab file format is used..


   - octave's -text and -ascii (are these different?) save in a format incompatible with matlab. I think it would be compatible if the # comment character in octave's save format was changed to %. Then both matlab and octave could load the file.



-ascii is on its way out and being replaced by "-text" exactly for this reason. Wait till 3.0 and this will be address. Till then use -mat-ascii...

I don't see the -mat-ascii switch available in the load/save helps. Also, it doesn't seem to be recognized in 2.9.3:
octave:13> save -mat-ascii test1 aa
warning: save: no such variable `test1'
octave:14>

will this be in 3.0?

Err, true... I was working from memory think that this had already been implemented. It hasn't yet, and you can only read matlab ascii files... So when the meaning of "-ascii" changes you'll get the matlab ascii file writing support. You could probably get something similar using fprintf till then if it is a strict requirement.




octave:59> a1=load('test2')
error: value on right hand side of assignment is undefined
error: evaluating assignment expression near line 59, column 3
octave:59>
fails.



See Dmitri's response...

D.

I read Dmitri's response, but he only reported that the above worked in 2.1.71. Have you tried to load a file like test2 in 2.9.3?


Just did... It worked fine with me... Are you sure there wasn't a hidden character in your file? To be sure I include as an attachement the version of the test file I used...

regards
David


8.0000000e+00 1.0000000e+00 6.0000000e+00 3.0000000e+00 5.0000000e+00 7.0000000e+00 4.0000000e+00 9.0000000e+00 2.0000000e+00





--------------070406020607010303070504--



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------