WAW WAW - 4 months ago 24
R Question

How to change matlab list into raster brick in R

I have this mat file I get from someone, and I wanted to change it to raster brick in R.

Importing was ok, I was able to import using the


library(rgdal)`enter code here`

List of 3
$ lonubn: num [1:231, 1] 34.6 34.6 34.6 34.6 34.6 ...
$ latubn: num [1:231, 1] 10.12 9.88 9.62 9.38 9.12 ...
$ Psim : num [1:8766, 1:231] NaN NaN NaN NaN NaN NaN NaN N NaN ...
- attr(*, "header")=List of 3
..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN64, Created
on: Fri Dec 19 10:10:41 2014 "
..$ version : chr "5"
..$ endian : chr "little"

In the lists are the lat, long, and Psim.

I used to raster brick function to convert to raster brick. However, I don't know the structure of the raster in the matlab, and it supposed to be a time series daily rainfall grid for 24 years. But I don't know why, I couldn't identify the number of layers from this mat file. I tried like brick from array, like

r <-brick(SM2RF$Psim, xmn = min(SM2RF[[1]]),xmx=max(SM2RF[[1]]), ymn = min(SM2RF[[2]]), ymx=max(SM2RF[[2]]),crs=CRS("+init=epsg:20136"))

However, I find this error.

Error in .local(x, ...) : cannot coerce a matrix to a RasterBrick

Could anyway help me ?
best regards


You could try something like:

x <- cbind(SM2RF$lonubn, SM2RF$latubn, t(SM2RF$Psim)) 
b <- rasterFromXYZ(x)