octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: saveing/loading symbol table of anonymous functions


From: David Bateman
Subject: Re: saveing/loading symbol table of anonymous functions
Date: Wed, 09 May 2007 15:17:51 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

David Bateman wrote:

> I think I can probably have saving/loading function handles work in
> matlab files by implementing loading/saving of objects, but only
> allowing it in a function handle by using a global variable to flag
> function handles in ls-mat5.cc. Its just a matter of finding the time to
> do it.. Yeah perhaps it'll be after 3.0 for the mat-files, but it really
> should be fixed for the octave native formats as the current state of
> the loading/saving is just broken. I just want to understand fully what
> matlab is doing so as not to have to change the code once again in the
> future...
> 
> mat-files that have function handles or objects currently load correctly
> with a warning about skipping the untreated variables.
> 
> D.
> 

I've been working on load/save for inline, function handles and
anonymous function handles in matlabs format. I have inline and function
handles working fine, but have difficulties with the format of anonymous
function handles. Basically this is a process of reverse engineering as
matlab doesn't document the matfile format for anonymous function
handles. The format is also excessively complex for what it is.

In any case I thought I would share what I found out about matlab's
matfile format when it includes a anonymous function. The major issue is
that anonymous function handles introduce a new class with an id of 17
(0x11) that I've named mxCLASS_WORKSPACE, and the format of this is
quite crazy. The mxCLASS_WORKSPACE class appears to be like a structure,

The matlab v5 file format for an anonymous function handle is like

mxCLASS_FUNCTION
  mxCLASS_STRUCT
    matlabroot (mxCLASS_CHAR "/opt/matlab73")
    separator (mxCLASS_CHAR "/")
    sentinel (mxCLASS_CHAR "@")
    function_handle
      mxCLASS_STRUCT
        function (mxCLASS_CHAR <string with anonymous function handle>)
        type (mxCLASS_CHAR "anonymous")
        file (mxCLASS_CHAR "")
        workspace (mxClassID = 17 [0x11])
          MCOS (mxCLASS_UINT32 5x1)
          function_handle_workspace (mxCLASS_UINT8 Nx1)

The class mxCLASS_WORKSPACE is like a mxCLASS_STRUCT but without
dimensions or the number of field names and length immediately following
the array flags. Instead there are always two field names with one of
them being MCOS and the other can change. The dimensions are assumed to
be (1,1).

The function_handle_workspace field is in fact a matfile in itself
stored as a UINT8 array which seems to contain a single structure. At
this point I have no idea what MCOS represents, and I haven't managed to
get octave to correctly read the function_handle_workspace variable,
which itself decomposes to

function_handle_workspace
 mxCLASS_STRUCT (1 field MCOS with no name!!!)
   MCOS (mxCLASS_WORKSPACE)
    MCOS (mxCLASS_CELL)
      [1,1] (mxCLASS_UINT8 Nx1)
      [2,1] (mxCLASS_DOUBLE 0x0)
      [3,1] (mxCLASS_CELL 2x1)
         [1,1] (mxCLASS_UINT32 5x1) ## Identical to
                                    ## function_handle.workspace.MCOS
         [2,1] (mxCLASS_STRUCT)  ## This has the real workspace in it
    FileWrapper__ (mxCLASS_UINT8 Nx1)
      MCOS (mxCLASS_DOUBLE [])

FileWrapper__ is also an embedded file like function_handle_workspace.
The only part of all of this mess that appears relevant is

function_handle.workspace.function_handle_workspace.MCOS.MCOS{3}{2}

that contains a structure with the local workspace of anonymous function
handle.. The field MCOS appears to be some sort of marker as the 5x1
cell element

function_handle.workspace.function_handle_workspace.MCOS.MCOS{3}{1}

is identical to function_handle.workspace.MCOS, however why the choice
of contents I have no idea.. I also have no idea what is

g.function_handle.workspace.function_handle_workspace.MCOS.MCOS{1}

though

char(g.function_handle.workspace.function_handle_workspace.MCOS.MCOS{1})

returns some interesting things.

At this point I could probably get some anonymous function handle load
save mat-file functionality running, but wouldn't mind some comments on
what people think are the other parts of this structure for a function
handle. I attach a loaded matlab anonymous function handle stored as a
structure in an octave text file the details of how they were created is

f = @(x) 2 * x;
save -v6 anon1.mat f
a = 2;
g = @(x) a + x;
save -v6 anon2.mat g

in matlab and then I loaded them in Octave with my work in progress
patch. Any comments on the contents of f and g in the attached file?

Regards
David





______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
# Created by Octave 2.9.10+, Wed May 09 14:43:21 2007 CEST <address@hidden>
# name: f
# type: struct
# length: 4
# name: function_handle
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 4
# name: file
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 0

# name: function
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 14
sf%0@(x) 2 * x

# name: type
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 9
anonymous

# name: workspace
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 2
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: uint32 matrix
# ndims: 2
 5 1
 3707764736
 2
 1
 1
 1

# name: function_handle_workspace
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 1
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 2
# name: FileWrapper__
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 1
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: matrix
# rows: 0
# columns: 0


# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: cell
# rows: 3
# columns: 1
# name: <cell-element>
# type: uint8 matrix
# ndims: 2
 200 1
 
 
 
 
 
 
 
 
 H
 
 
 
 h
 
 
 
 €
 
 
 
 °
 
 
 
 ¸
 
 
 
 È
 
 
 
 
 
 
 
 
 
 
 
 a
 n
 y
 
 f
 u
 n
 c
 t
 i
 o
 n
 _
 h
 a
 n
 d
 l
 e
 _
 w
 o
 r
 k
 s
 p
 a
 c
 e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
# name: <cell-element>
# type: matrix
# rows: 0
# columns: 0
# name: <cell-element>
# type: cell
# rows: 2
# columns: 1
# name: <cell-element>
# type: uint32 matrix
# ndims: 2
 5 1
 3707764736
 2
 1
 1
 0
# name: <cell-element>
# type: struct
# length: 0







# name: matlabroot
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 13
/opt/matlab73

# name: sentinel
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 1
@

# name: separator
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 1
/

# name: g
# type: struct
# length: 4
# name: function_handle
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 4
# name: file
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 0

# name: function
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 14
sf%0@(x) a + x

# name: type
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 9
anonymous

# name: workspace
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 2
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: uint32 matrix
# ndims: 2
 5 1
 3707764736
 2
 1
 1
 1

# name: function_handle_workspace
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 1
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 2
# name: FileWrapper__
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: struct
# length: 1
# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: matrix
# rows: 0
# columns: 0


# name: MCOS
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: cell
# rows: 3
# columns: 1
# name: <cell-element>
# type: uint8 matrix
# ndims: 2
 200 1
 
 
 
 
 
 
 
 
 H
 
 
 
 h
 
 
 
 €
 
 
 
 °
 
 
 
 ¸
 
 
 
 È
 
 
 
 
 
 
 
 
 
 
 
 a
 n
 y
 
 f
 u
 n
 c
 t
 i
 o
 n
 _
 h
 a
 n
 d
 l
 e
 _
 w
 o
 r
 k
 s
 p
 a
 c
 e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
# name: <cell-element>
# type: matrix
# rows: 0
# columns: 0
# name: <cell-element>
# type: cell
# rows: 2
# columns: 1
# name: <cell-element>
# type: uint32 matrix
# ndims: 2
 5 1
 3707764736
 2
 1
 1
 0
# name: <cell-element>
# type: struct
# length: 1
# name: a
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: scalar
2








# name: matlabroot
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 13
/opt/matlab73

# name: sentinel
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 1
@

# name: separator
# type: cell
# rows: 1
# columns: 1
# name: <cell-element>
# type: sq_string
# elements: 1
# length: 1
/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]