1

I received 3 .mat files that each contain several daily maps. I need to take them and get them into ArcMap (10.2 if that matters) as a .tif with spatial information. I have already used the R.matlab package to read the .mat files and successfully did it for one day, however I do not want to have to write hundreds of lines of code to repeat it for the remaining days. Here is my successful code:

library(R.matlab)
library(raster)
library(sp)
library(rgdal)

setwd("C:/Users/Heidi Church/Documents/Thesis/FSLE")

FSLE2006mat <- readMat("FSLEheidi2006.mat")
FSLE2007mat <- readMat("FSLEheidi2007.mat")
FSLE2008mat <- readMat("FSLEheidi2008.mat")

#Test on single day in 2006
x <- raster(FSLE2006mat$F2006812, xmn=152, xmx=155.5, ymn=47.5, ymx=50.5, crs="+proj=longlat +datum=WGS84")
plot(x, xlab="Latitude", ylab="Longitude", xlim=c(152,155.5), ylim=c(47.5,50.5), main="Submesoscale Fronts: August 12, 2006")
writeRaster(x, filename="FSLE_20060812.tif", format="GTiff", overwrite=TRUE)

I had tried doing both a for loop and lapply, however kept getting this error:

Error in .local(x, ...) : unused arguments (xmn = 152, xmx = 155.5, ymn = 47.5, ymx = 50.5, crs = "+proj=longlat +datum=WGS84")

Here are examples of each that I tried:

for (i in FSLE2006mat[1:16])
{
FSLE2006[i] <- raster(x=FSLE2006mat[i], xmn=152, xmx=155.5, ymn=47.5, ymx=50.5, crs="+proj=longlat +datum=WGS84")
}

FSLE2006 <- lapply(FSLE2006mat, (raster(x, xmn=152, xmx=155.5, ymn=47.5, ymx=50.5, crs="+proj=longlat +datum=WGS84")))
FSLE2006 <- lapply(FSLE2006mat, raster)
FSLE2006 <- lapply(FSLE2006mat[1:16], raster)

I am wondering how I can loop through all of the elements in the .mat list for each year, make them a raster with the spatial info noted, and write it to a .tif file to import into ArcGIS. I should also note that 1:16 is to only use the first 16 of 18 elements, as the last 2 are not needed.

9
  • what is the structure of objects you read in? It's unlikely that raster() understands them, unless they strictly follow the list(x, y, z) structure understood by ?image. I.e. what is str(FSLE2006mat$F2006812)
    – mdsumner
    Commented Apr 30, 2015 at 12:46
  • And btw, surely matlab can write a decent raster file like GeoTIFF or similar rather than just a dump of its workspace. That might be a better pathway, why is R involved at all?
    – mdsumner
    Commented Apr 30, 2015 at 12:48
  • It's numeric (num [1:301, 1:351]). The first code worked correctly, so that is not the problem. The problem is that I don't know how to do that for all of the dates in each .mat file at once. I do not use Matlab, so I needed to go through R. Commented Apr 30, 2015 at 17:14
  • If you replace $F2006812 with [1] in your working code this works as well? I'm not familiar with .mat so I'm wondering if the indexs are giving you the columns you want.
    – jbosq
    Commented Apr 30, 2015 at 18:40
  • No, that didn't work. So I guess that is my problem. F20060812test <- raster(FSLE2006mat[1], xmn=152, xmx=155.5, ymn=47.5, ymx=50.5, crs="+proj=longlat +datum=WGS84") Error in .local(x, ...) : unused arguments (xmn = 152, xmx = 155.5, ymn = 47.5, ymx = 50.5) Commented Apr 30, 2015 at 19:42

2 Answers 2

0

Heidi's code may work, but is not very general. it still requires to copy/paste/edit the code for different years. I do not have the data, so I cannot check the below, but I would do something like this:

rasterFromMat <- function(m, crs="") {
    r <- raster(m[[1]], xmn=min(m$xvc), xmx=max(m$xvc), ymn=min(m$yvc), ymx=max(m$yvc), crs=crs)
    names(r) <- names(m[1])
    r
}

stackFromMat <- function(x, crs="+proj=longlat +datum=WGS84") {
    s <- lapply(1:(length(x)-2), function(i) rasterFromMat(x[[i]], crs=crs))
    stack(s)
}


s2006 <- stackFromMat(FSLE2006mat)
s2006 <- writeRaster(s2006, "FSLE2006.tif", overwrite=TRUE)

s2007 <- stackFromMat(FSLE2007mat)
s2007 <- writeRaster(s2007, "FSLE2007.tif", overwrite=TRUE)

Note that it might be useful not to write individual layers --- depending on your next steps, of course.

1
  • The code you just provided requires the same amount of copy & pasting Commented May 5, 2015 at 18:44
-1

I have found a script that works for my question. This is the exact script that worked correctly, so please do not edit. For anyone with similar problems, here is what did work:

### Read matlab files in R
library(R.matlab)

FSLE2006mat <- readMat("FSLEheidi2006.mat")
FSLE2007mat <- readMat("FSLEheidi2007.mat")
FSLE2008mat <- readMat("FSLEheidi2008.mat")


### Make matlab files into rasters
library(raster)
library(sp)

### Script to loop through each date in 2006, make a raster, and stack them with 
### dates included in the names for each layer - THANKS TO BILL BEATTY AT USGS

r.1 <- raster(FSLE2006mat[[1]], xmn=min(FSLE2006mat$xvc), xmx=max(FSLE2006mat$xvc),
          ymn=min(FSLE2006mat$yvc), ymx=max(FSLE2006mat$yvc), 
          crs="+proj=longlat +datum=WGS84")
r.1@data@names <- names(FSLE2006mat[1])

for (i in 2:((length(FSLE2006mat))-2)) {
  r <- raster(FSLE2006mat[[i]], xmn=min(FSLE2006mat$xvc), xmx=max(FSLE2006mat$xvc),
          ymn=min(FSLE2006mat$yvc), ymx=max(FSLE2006mat$yvc), 
          crs="+proj=longlat +datum=WGS84")
  r@data@names <- names(FSLE2006mat[i])
  r.1  <- stack(r.1, r)
}

writeRaster(r.1, "FSLE2006.tif", format="GTiff", bylayer=T, suffix=names(r.1), overwrite=T)

I then was able to simply go through and use that same code for 2007 and 2008. In the end, this spat out .tif files for each date that I was able to open in R with all the specified spatial reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.