[R] Matlab import

Spencer Graves spencer.graves at pdf.com
Fri Apr 20 00:09:30 CEST 2007


      Have you tried 'getVariable' from Matlab?  I also recently failed 
to get 'readMat' to work for me -- probably because I hadn't saved the 
files using the options Henrik suggested below. 

      Fortunately, I was able to get something like the following to work: 

# Start Matlab server
Matlab$startServer()

# Create a Matlab client object used to communicate with Matlab
matlab <- Matlab()

# Get max info for diagnosis
setVerbose(matlab, -2)

# Confirm that 'matlab' is running
open(matlab)

# Load the raw input data in matFile.mat into Matlab
evaluate(matlab, "load matFile")

# Transfer it to R. 
varInMatfile <- getVariable(matlab, "variableInMatFile")
 
# Done
close(matlab)

      This was with Matlab 7.3.0 (R2006b) and the following versions of 
everything else: 

 sessionInfo()
R version 2.4.1 (2006-12-18)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods" 
[7] "base"    

other attached packages:
R.matlab     R.oo
 "1.1.3"  "1.2.7"

       Hope this helps. 
      Spencer Graves

Henrik Bengtsson wrote:
> Hi,
>
> as already mentioned, do not save MAT files in ASCII format but save
> to binary formats, i.e. do *not* use "-ascii".  Moreover, from
> ?readMat, you find that:
>
>      "From Matlab v7, _compressed_ MAT version 5 files are used by
>      default [3]. These are not supported. Use 'save -V6' in Matlab to
>      write a MAT file compatible with Matlab v6, that is, to write a
>      non-compressed MAT version 5 file. Note: Do not mix up version
>      numbers for the Matlab software and the Matlab file formats."
>
> You haven't told use what version of R you are using (neither what
> version of R.matlab), but from the error message I suspect you are
> using Matlab v7, correct?  If so, try to save with
>
>    save('test.mat', 'matrixM', '-ascii', '-V6')
>
> and tell us if it works.
>
> Cheers
>
> Henrik
>
>
> On 4/10/07, Schmitt, Corinna <Corinna.Schmitt at igb.fraunhofer.de> wrote:
>   
>> Hallo,
>>
>>     
>>> With readMat, don't use the "-ascii" option (which you didn't have in your
>>> first posting). I've never tried reading matlab's ascii format. In any >case,
>>> readMat reads matlab's binary format.
>>>       
>>> - Tom
>>>       
>> I did the saving again without 'ascii' option but the import also did not work. I get the following error message:
>>
>>     
>>> library(R.matlab)
>>> mats <- readMat("Z:/Software/R-Programme/test2.dat")
>>>       
>> Fehler in list("readMat("Z:/Software/R-Programme/test2.dat")" = <environment>,  :
>>
>> [2007-04-10 14:57:52] Exception: Tag type not supported: miCOMPRESSED
>>   at throw(Exception(...))
>>   at throw.default("Tag type not supported: ", tag$type)
>>   at throw("Tag type not supported: ", tag$type)
>>   at readMat5DataElement(this)
>>   at readMat5(con, firstFourBytes = firstFourBytes, maxLength = maxLength)
>>   at readMat.default("Z:/Software/R-Programme/test2.dat")
>>   at readMat("Z:/Software/R-Programme/test2.dat")
>>     
>> Any further idea,
>> Corinna
>>
>> ----------------------------------------------------------------------------
>>
>>
>> Schmitt, Corinna wrote:
>>     
>>> Hallo,
>>>
>>>       
>>>> I've used Henrik Bengtsson's R.matlab package several times to
>>>>         
>> successfully
>>     
>>>> read in matlab data files. It's normally as easy as:
>>>>         
>>>> library(R.matlab)
>>>> mats <- readMat("matrixM.txt")
>>>>         
>>>> - Tom
>>>>         
>>> I have imported this package, too. And tried your commands with the new
>>> txt-file as mentioned in my last mail to the mailing list.
>>>
>>> I get the following error command:
>>> mats = readMat("Z:/Software/R-Programme/test.dat")
>>> Error in if (version == 256) { : Argument hat Länge 0
>>> Zusätzlich: Warning message:
>>> Unknown endian: . Will assume Bigendian. in: readMat5Header(this,
>>> firstFourBytes = firstFourBytes)
>>>
>>> What did I wrong? Please check my txt-file which was constructed with the
>>> matlab command "save('test.dat','matrixM','-ascii')"
>>>
>>> Thanks, Corinna
>>>
>>> ----------------------------------------------------------------------------
>>>
>>>
>>> Schmitt, Corinna wrote:
>>>       
>>>> Dear R-Experts,
>>>>
>>>> here again a question concerning matlab. With the command "matrixM=[1 2
>>>> 3;4 5 6]" a matrix under Matlab was constructed. It was than stored with
>>>> the command "save('matrixM.txt','matrixM')".
>>>>
>>>> Now I tried to import the data in R with the help of the command
>>>> "Z=matrix(scan("Z:/Software/R-Programme/matrixM.txt"))"
>>>>
>>>> An error occurred.
>>>>
>>>> The result should be a matrix with the entries as mentioned above.
>>>> Perhaps I made already an error in matlab.
>>>>
>>>> Has any one got an idea how to import the data and store it in R. In R I
>>>> want to make further calculations with the matrix. I just installed
>>>> R.matlab but could not find an example with could help me.
>>>>
>>>> Thanks, Corinna
>>>>
>>>> MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Tue Apr 10 13:17:44
>>>> 2007
>>>> �IM���3���xœãc``p�b6 æ€Ò À
>>>> å31331;ç&–eVø‚ÅAjY˜X™
>>>>         
>>> �[n|
>>>       
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch 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.
>>>>
>>>>
>>>>         
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Matlab-import-tf3552511.html#a9918327
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch 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.
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch 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.
>>>
>>>
>>>       
>> --
>> View this message in context: http://www.nabble.com/Matlab-import-tf3552511.html#a9918787
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
>>     
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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