[R] Help needed in reading matlab files

Rui Barradas ruipbarradas at sapo.pt
Tue Aug 14 20:12:39 CEST 2012


Hello,

I don't have MATLAB here so this is completely untested but the help 
page for readMat says:


      Arguments

|con| 	

Binary |connection 
<http://127.0.0.1:11101/library/base/html/connections.html>| to which 
the MAT file structure should be written to. A string is interpreted as 
filename, which then will be opened (and closed afterwards).


So it should recognize a filename and open it as a binary connection. If 
it doesn't, try

adj_con <- file(pathnames_adj, open = "b")
mat_adj <- readMat(adj_con)
[...etc...]
close(adj_con)

Hope this helps,

Rui Barradas

Em 14-08-2012 12:54, jerome at kosta.ch escreveu:
> Dear users,
>
> I am stucked with a programming problem: I am trying to download a squared adjacency matrix from matlab with only 0 or 1. Doing it without a loop at first, I get an error message that is, I think, related to the  "mat_adj<-readMat(pathnames_adj)" line. Did anybodz encouter already that problem? If so, do I need to change anything in my code in order it not to be recurrent? If I run the code a second time, it works fine, but this does not help me, as I try to set up a loop for 265 matlab adj matrix, each number representing a specific week and the purpose being to have metrics from igraph being generated over the 265 weeks.
>
> Thank you
>
> CODE to read the file:
>
> rm(list=ls())
> library(R.matlab)
> path<-setwd("V:/2012/nonweighted_GOVSOV")
> pattern_adj<-sprintf("weightmatrix1.mat")
> pathnames_adj<-list.files(pattern=pattern_adj,path=path,full.names=T)
> cat("Reading MAT file: ", basename(pathnames_adj), "\n", sep="")
> mat_adj<-readMat(pathnames_adj)
> adj<-do.call(rbind,mat_adj)
> library(igraph)
> g<-graph.adjacency(adj, mode=c("directed"), weighted=T)
>
> _______________________________________________________________________
>
> R RESPONSE:
>
>> path<-setwd("V:/2012/nonweighted_GOVSOV")
>> pattern_adj<-sprintf("weightmatrix1.mat")
>> pathnames_adj<-list.files(pattern=pattern_adj,path=path,full.names=T)
>> cat("Reading MAT file: ", basename(pathnames_adj), "\n", sep="")
> Reading MAT file:
>> mat_adj<-readMat(pathnames_adj)
> Error in file(con, open = "rb") : invalid 'description' argument
>> adj<-do.call(rbind,mat_adj)
> Error in do.call(rbind, mat_adj) : object 'mat_adj' not found
>> library(igraph)
> Attaching package: ‘igraph’
>
> The following object(s) are masked from ‘package:R.oo’:
>
>      hierarchy
>
>> g<-graph.adjacency(adj, mode=c("directed"), weighted=T)
> Error in inherits(adjmatrix, "Matrix") : object 'adj' not found
>
> ______________________________________________
> 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