[R] R and Matlab
Henrik Bengtsson
hb at stat.berkeley.edu
Fri Oct 29 06:25:01 CEST 2010
Hi,
thanks for reporting on a potential issue with writeMat() in R.matlab.
However, I think you are blaming the wrong source here. There is
basically nothing wrong with writeMat() and the MAT files written by
it can indeed be read by Matlab.
I think you are experiencing two different problems.
PROBLEM #1: The 'flu' object in package 'hyperSpec' is an S4 object
> library("hyperSpec");
> str(flu);
Formal class 'hyperSpec' [package ".GlobalEnv"] with 4 slots
..@ wavelength: num [1:181] 405 406 406 406 407 ...
..@ data :'data.frame': 6 obs. of 2 variables:
...
It is not a data.frame or any other basic R data types. This is why:
writeMat("flu.mat", flu);
gives an error. To get a data.frame representation of 'flu' you can do:
df <- as.data.frame(flu);
and you can write it as:
writeMat("flu.mat", flu=df);
It may be that in a previous version of hyperSpec 'flu' used to be a
data frame, which may explain why "it used to work".
PROBLEM #2: You write non-named objects, cf. argument '...' help("writeMat").
Non-named objects to be written by writeMat() will not be named inside
the MAT file. For example,
x <- 1:10;
writeMat("foo.mat", x);
will write the values 1:10 *unnamed* to 'foo.mat', whereas
writeMat("foo.mat", x=x);
will write them names as "x". You can also name them something else, e.g.
writeMat("foo.mat", y=x);
which if you load the file in Matlab will generate a variable 'y' with
values 1:10.
Maybe it is more clear if I say that technically,
x <- 1:10;
writeMat("foo.mat", x);
is the same as
writeMat("foo.mat", 1:10);
Note that in Matlab, this will make 'load foo.mat' to load the MAT
file but since the object is not named it will not be stored. This
has most likely always been the case, so I don't think your statement
that "it used to work" is correct. Note that the functional form data
= load('foo.mat') in Matlab will still work just as data <-
readMat("foo.mat") does in R.
FYI, in next release, R.matlab v1.3.5, writeMat() will give an
informative warning about this, e.g.
> writeMat("flu.mat", x)
Warning message:
In writeMat.default("flu.mat", x) :
All objects written have to be named, e.g. use writeMat(..., x=a,
y=y) and not writeMat(..., x=a, y): writeMat.default("flu.mat", x)
I intentionally do not try to "infer" the name "x" from
writeMat("flu.mat", x), basically because I think using substitute()
should be avoided as far as possible, but also because it is unclear
what the name should be in cases such as writeMat("flu.mat", 1:10).
MISCELLANEOUS:
Note that writeMat() cannot write compressed MAT files. It is
documented in help("readMat"), and will be so in help("writeMat") in
the next release. Package Rcompression, loaded or not, has no effect
on writeMat(). It is only readMat() that can read them, if
Rcompression is installed. You do not have to load it
explicitly/yourself - if readMat() detects a compress MAT file, it
will automatically try to load it;
/Henrik
On Thu, Oct 28, 2010 at 8:16 AM, Claudia Beleites <cbeleites at units.it> wrote:
> On 10/28/2010 03:16 PM, Thomas Levine wrote:
>>
>> Is there a particular reason you can't use csv?
>
> (Not sure whether I'm meant - as I also suggested csv to Santosh)
>
> But:
>
> - It used to work, so there may be code existing that is broken now (e.g. I
> do
> have such code, but at least for the moment it doesn't matter). Thus the
> information may very well be of interest for the maintainer.
>
> - csv is fine for a matrix (or a vector) or a data.frame. How about arrays,
> lists, more than one variable?
>
> I think the default file format changed to v7.3 (though I'm not sure whether
> that is just for large variables).
> Unfortunately -v switch of load (that used e.g. to allow reading of V4
> files) is
> gone, and I can't see anything to specify the .mat file format version.
> The curious thing is that readMat does accept the file produced by Matlab
> 2008b.
> If it is a matter of writeMat writing an old file format, I'd have expected
> that
> rather load should still be able to read the writeMat generated file than
> readMat being able to read Matlab's .mat file.
>
> my 2 ct
>
> Claudia
>
>>
>> write.csv() in R
>>
>> It seems that you can read csv in Matlab with this
>> http://www.mathworks.com/help/techdoc/ref/importdata.html
>>
>> Tom
>>
>> 2010/10/28 Claudia Beleites<cbeleites at units.it>:
>>>>
>>>> I am looking for ways to use R and Matlab. Doing the data
>>>> transformations
>>>> in
>>>> R and using the data in Matlab to analyze with some pre-defined scripts.
>>>> Any good ways to transfer the data into matlab in its most recent
>>>> version?
>>>> I tried using R.matlab but the writeMat output is not readable by
>>>> Matlab.
>>>
>>> It used to work, but I didn't need it for quite a while (a year or so
>>> ago,
>>> and with Matlab either 2007 or 2008a).
>>>
>>> I just tried, and neither does it work for me.
>>> You should notify the maintainer of R.matlab and include an example (code
>>> and data, e.g. with dput).
>>>
>>> I noticed that library (R.matlab) does not load the Rcompression package,
>>> but also after library (Rcompression), the resulting file was not read by
>>> Matlab.
>>>
>>> I tried loading a saved data.frame in Matlab 2008b on an Win XP computer:
>>> it
>>> doesn't find any variables inside the .mat file (and whos -file ...)
>>> doesn't
>>> show a variable.
>>>
>>> The other way round with a stupid little vector it worked.
>>>
>>> An R session (with only the 2nd try, after library (Rcompression)) is
>>> attached below.
>>>
>>>
>>>> I just need to output a data.frame and read it as is into matlab where I
>>>> can
>>>> do any needed transformations on the variables.
>>>
>>> If you need to transfer the data right NOW, there's always csv.
>>>
>>> Claudia
>>>
>>> ********************
>>>
>>>> library (hyperSpec)
>>>
>>> Loading required package: lattice
>>> Package hyperSpec, version 0.95
>>>
>>> To get started, try
>>> vignette ("introduction", package = "hyperSpec")
>>> package?hyperSpec
>>> vignette (package = "hyperSpec")
>>>
>>> If you use this package please cite it appropriately.
>>> citation("hyperSpec")
>>> will give you the correct reference.
>>>
>>> The project is hosted on http://r-forge.r-project.org/projects/hyperspec/
>>>
>>>> sessionInfo ()
>>>
>>> R version 2.12.0 (2010-10-15)
>>> Platform: x86_64-pc-linux-gnu (64-bit)
>>>
>>> locale:
>>> [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C
>>> LC_TIME=en_US.utf8
>>> [4] LC_COLLATE=en_US.utf8 LC_MONETARY=C
>>> LC_MESSAGES=en_US.utf8
>>> [7] LC_PAPER=en_US.utf8 LC_NAME=C LC_ADDRESS=C
>>> [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.utf8
>>> LC_IDENTIFICATION=C
>>>
>>> attached base packages:
>>> [1] stats graphics grDevices utils datasets methods base
>>>
>>> other attached packages:
>>> [1] hyperSpec_0.95 lattice_0.19-13 R.matlab_1.3.3 R.oo_1.7.4
>>> R.methodsS3_1.2.1
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.12.0
>>>>
>>>> library (Rcompression)
>>>> x = flu[[]]
>>>> writeMat ("flu.mat", flu)
>>>
>>> Error in dim(x)<- length(x) : invalid first argument
>>>>
>>>> writeMat ("flu.mat", x)
>>>> sessionInfo ()
>>>
>>> R version 2.12.0 (2010-10-15)
>>> Platform: x86_64-pc-linux-gnu (64-bit)
>>>
>>> locale:
>>> [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C
>>> LC_TIME=en_US.utf8
>>> [4] LC_COLLATE=en_US.utf8 LC_MONETARY=C
>>> LC_MESSAGES=en_US.utf8
>>> [7] LC_PAPER=en_US.utf8 LC_NAME=C LC_ADDRESS=C
>>> [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.utf8
>>> LC_IDENTIFICATION=C
>>>
>>> attached base packages:
>>> [1] stats graphics grDevices utils datasets methods base
>>>
>>> other attached packages:
>>> [1] Rcompression_0.8-0 hyperSpec_0.95 lattice_0.19-13
>>> R.matlab_1.3.3
>>> R.oo_1.7.4
>>> [6] R.methodsS3_1.2.1
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.12.0
>>>
>>>
>>>
>>> --
>>> Claudia Beleites
>>> Dipartimento dei Materiali e delle Risorse Naturali
>>> Università degli Studi di Trieste
>>> Via Alfonso Valerio 6/a
>>> I-34127 Trieste
>>>
>>> phone: +39 0 40 5 58-37 68
>>> email: cbeleites at units.it
>>>
>>> ______________________________________________
>>> 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.
>>>
>
>
> --
> Claudia Beleites
> Dipartimento dei Materiali e delle Risorse Naturali
> Università degli Studi di Trieste
> Via Alfonso Valerio 6/a
> I-34127 Trieste
>
> phone: +39 0 40 5 58-37 68
> email: cbeleites at units.it
>
> ______________________________________________
> 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