[R] writeMat error

Henrik Bengtsson hb at stat.berkeley.edu
Wed Oct 1 21:55:13 CEST 2008


Hi.

On Sat, Sep 27, 2008 at 5:17 AM, Steele, Dr Douglas <d.steele at abdn.ac.uk> wrote:
> Hi
>
> I am using Ubuntu 8.04 64 bit, R as below, Matlab 7.6.0.  I would like to transfer mat files back and forward between R and Matlab.  Whilst I have used Matlab for years its been a long time since I have used R (hence question may be a bit simple)
>
> Running code
>
> A  <-  c(1:10)
> dim(A) <- c(2,5)
> library(R.matlab)
> writeMat('A.mat', A=A)
>
> Does not appear to generate any mat file either in local directory or in /tmp/R*

It *is* created in the *current* directory.  If there are no errors,
then it should be there.  Use can use getwd() to see which this is,
e.g.

library("R.matlab");
A  <-  c(1:10);
dim(A) <- c(2,5);
writeMat("A.mat", A=A);
print(getwd());

[1] "C:/Users/foo/braju.com.R/R.matlab"

print(list.files())
[1] "A.mat"

res <- readMat("A.mat");
> str(res)
List of 1
 $ A: int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
 - attr(*, "header")=List of 3
  ..$ description: chr "MATLAB 5.0 MAT-file, Platform: windows,
Software: R v2.7.2, Created on: Wed Oct 01 12:40:23 2008
             "
  ..$ version    : chr "5"
  ..$ endian     : chr "little"

>
> I tried both current version of R.matlab and older version because of bug
> https://stat.ethz.ch/pipermail/r-help/2008-September/173432.html

Correct.  You should use install the old R.matlab v1.2.1, because
v1.2.3 (as well as v1.2.2) contains a bug which is still to be fixed.

>
> A <- matrix(1:27, ncol=3)
> B <- as.matrix(1:10)
> fn <- paste(tempfile(), ".mat", sep="")
> writeMat(fn, A=A, B=B)
> data <- readMat(fn)
> print(data)
> unlink(fn)

This is from example(writeMat).  It works this far, correct?  You
should see 'data' being reported.  The unlink() command is there such
that the example script cleans up after itself, i.e. it delete the
temporary MAT file. Skip that last command if you want to grab the
file.

> onWrite <- function(x)
> writeBin(x$length, con=x$con, size=4, endian="big");
> writeMat(fn, A=A, B=B, onWrite=onWrite)

Do *not* use these latter commands.  They are modifications of
original example code that is only used to illustrate for advanced
users how to send a MAT structure over a connection.  As indicated by
the example code:

     ## Not run:
     # When writing to a stream connection the receiver needs to know in
     # beforehand how many bytes are available. This can be done by using
     # the 'onWrite' argument.
     onWrite <- function(x)
       writeBin(x$length, con=x$con, size=4, endian="little");
     writeMat(con, A=A, B=B, onWrite=onWrite)
     ## End(Not run)

they are within a "Not run" block, which in R means that they are not
necessarily intended to be executed.  If you run the example code with
example(writeMat) that code will not be executed.  You obviously
cut'n'paste here, but didn't you see the above comments.  How you do
view you help(), i.e. typically help(writeMat) will display those
comments?  FYI, the first argument 'con' (supposed to indicate a
connection) is on purpose non-existing and not the same as the 'fn'
argument (pathname) in the preceeding code.

>
> Does generate a file in /tmp/Rtmpjhfjxw
> -rw-r--r--  1 dst dst  396 2008-09-27 12:46 file643c9869.mat
>
> When I try to read it into Matlab I get
>
> ??? Error using ==> load
> Unable to read MAT-file /tmp/Rtmpjhfjxw/file643c9869.mat
>
> File may be corrupt.

Yes, but that is because you use that latter code.  Before writing the
MAT structure, it writes the byte size of the structure to the file,
which is not part of the MAT file format.  You don't want this.  This
is only intended for sending MAT structure over a connection.

>
> (This is same error message as above link and occurs with old and current version of R.matlab)
>
> Any advice would be very helpful

Hope this helps

Henrik

>
> Regards
>
> Douglas
>
>
>
>> sessionInfo()
> R version 2.6.2 (2008-02-08)
> x86_64-pc-linux-gnu
>
> locale:
> LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] R.matlab_1.2.1    R.oo_1.4.6        R.methodsS3_1.0.3
>
> loaded via a namespace (and not attached):
> [1] rcompgen_0.1-17
>>
>
>
> The University of Aberdeen is a charity registered in Scotland, No SC013683.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list