R.matlab icon indicating copy to clipboard operation
R.matlab copied to clipboard

Feature request: Make it possible to retrieve a field of a structure

Open HenrikBengtsson opened this issue 10 years ago • 0 comments

As reported in Issue #5, it is not possible to retrieve a field of a Matlab structure using getVariable(), e.g. getVariable(matlab, "x.a") This is simply because the field is not a Matlab variable by itself. The workaround is to assign to a temporary variable and retrieve that, e.g.

evaluate(matlab, "tmp = x.a;")
x.a <- getVariable(matlab, "tmp")

Full details below.

A solution would possibly be to make getVariable() handle also getVariable(matlab, "x.a") or alternatively adding getField(matlab, "x.a") or getValue(matlab, "x.a").

Example

Starting and connection to Matlab server

> library("R.matlab")
> Matlab$startServer()
[1] 0

Warning: No window system found.  Java option 'MWT' ignored

                            < M A T L A B (R) >
                  Copyright 1984-2012 The MathWorks, Inc.
                    R2012a (7.14.0.739) 64-bit (glnxa64)
                              February 9, 2012

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

Running MatlabServer v3.0.2
MATLAB v7.x or higher detected.
Saving with option -V6.
Added InputStreamByteWrapper to dynamic Java CLASSPATH.
----------------------
MATLAB server started!
----------------------
MATLAB working directory: /home/henrik/tmp
Trying to open server socket (port 9999)...done.
Waiting for client to connect (port 9999)...

> matlab <- Matlab()
> if (!isOpen(matlab)) open(matlab)
[1] TRUE
> connected.

Setting up a Matlab structure

> evaluate(matlab, "x = struct('a', 1:2);")
Received cmd: 1
"eval" string: "x = struct('a', 1:2);"
Sent byte: 0

> evaluate(matlab, "x")
Received cmd: 1
"eval" string: "x"

x =

    a: [1 2]

Sent byte: 0

> evaluate(matlab, "x.a")
Received cmd: 1
"eval" string: "x.a"

ans =

     1     2

Sent byte: 0

Retrieving it

> x <- getVariable(matlab, "x")
Received cmd: 1
"eval" string: "variables = {'x'};"
Sent byte: 0
Received cmd: 2
save(tmpname, '-V6', 'x')
> answer=0

> x
$x
, , 1

  [,1]
a Numeric,2


attr(,"header")
attr(,"header")$description
[1] "MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Wed Dec 17 13:50:08 2014                                        "

attr(,"header")$version
[1] "5"

attr(,"header")$endian
[1] "little"

Retrieving a specific field of a structure

It is not possible to retrieve a field of a structure directly, e.g.

> x.a <- getVariable(matlab, "x.a")
Received cmd: 1
"eval" string: "variables = {'x.a'};"
Sent byte: 0
Received cmd: 2
Variable 'x.a' not found.
Error in readChar(con = con, nchars = nbrOfBytes) :
  invalid 'nchars' argument

(Here you actually need to restart the Matlab server etc. to get things working again, cf. Issue #7.)

Workaround: Assign the field to an temporary variable and retrieve that, e.g.

> evaluate(matlab, "tmp = x.a;")
Received cmd: 1
"eval" string: "tmp = x.a;"
Sent byte: 0
> x.a <- getVariable(matlab, "tmp")
Received cmd: 1
"eval" string: "variables = {'tmp'};"
Sent byte: 0
Received cmd: 2
save(tmpname, '-V6', 'tmp')
> answer=0

> x.a <- getVariable(matlab, "tmp")
Received cmd: 1
"eval" string: "variables = {'tmp'};"
Sent byte: 0
Received cmd: 2
save(tmpname, '-V6', 'tmp')
> answer=0

> x.a
$tmp
     [,1] [,2]
[1,]    1    2

attr(,"header")
attr(,"header")$description
[1] "MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Wed Dec 17 14:00:33 2014                                        "

attr(,"header")$version
[1] "5"

attr(,"header")$endian
[1] "little"

HenrikBengtsson avatar Dec 17 '14 22:12 HenrikBengtsson