Applying prepared script for one file to many files in a directory?

#1
I am having a problem with the main loop.it succeeded to finish reading and writing from the first file in my listfile(12 files) but failed when it moves to the second file giving this error:Error: subscript out of bounds.I think my problem is with K but I dont know how to solve it.Any help please
Code:
## my code goes here
 library(Matrix)
   setwd("img")
       listfile<-dir()
   long <- file("inra.bin", "rb")
   A=readBin(long, integer(), size=2,n=67420*1, signed=F)
  ta<-t(A)
 lot <- file("lat.img", "rb")
   B=readBin(lot, integer(), size=2,n=67420*1, signed=F)
     tb<-t(B)

         for (n in listfile)
             {

             b=file.info(n)$size/67420/4

       wind <- file(n, "rb")
       C=readBin(wind, double(), size=4,n=67420*b, signed=TRUE)

        D<-matrix(C,nrow=b,ncol=67420)

          for(kin 1:b)
       {
      M <- Matrix(-9999, 360, 720)
    tm<-t(M)
      for(i in 1:67420)
      {
                   tm[ta[i],tb[i]]= round(10 * mean(D[(k-1)*8 + 1:8), i])

      }
      to.write <- sprintf("\Yar_%00d.bin", k)
      writeBin(as.integer(tm@x), size=2,to.write)}}
 
Last edited:

Dason

Ambassador to the humans
#2
You posted this on SO a while ago. I didn't like it there either because I don't know what you were thinking when you indented this code.

Also - the way your question is it's pretty hard to diagnose because we don't have the same directory contents as you.

You have three loops but I only see one closing brace.

Could the problem be that the second file in the directory doesn't have the same dimensions as the first? Like I said it's impossible to tell. I would suggest walking through the loops yourself. So set n=dir()[2] and then see where the problem occurs...

I would also HIGHLY recommend learning how to indent your code properly. If you post code here you can use something like

[noparse]
Code:
# My code goes here
a <- rnorm(20)
for(i in 1:10){
    print("LOL") # This is actually indented
}
[/noparse]

and that will give you:

Code:
# My code goes here
a <- rnorm(20)
for(i in 1:10){
    print("LOL") # This is actually indented
}
 

Dason

Ambassador to the humans
#3
Have you tried walking through the code like I suggested using n=dir()[2]

Like I mentioned before it's not going to be easy for us to help you because you're code is written in a way that it's highly dependent on your system.

Also your indentation really needs work. Is that how your source file actually looks?
 

bryangoodrich

Probably A Mammal
#4
WRONG
Code:
 library(Matrix)
   setwd("C:\\Users\\puppy\\Desktop\\img")
       listfile<-dir()
RIGHT
Code:
library(Matrix)
setwd("C:\\Users\\puppy\\Desktop\\img")
listfile<-dir()
If you post code like that again I'll beat you to death with a programming book!

BTW, you have "for(kin ...)" you need to check your syntax as it is supposed to be "for(k in ...)"
 
Last edited by a moderator:

Dason

Ambassador to the humans
#5
General list of suggestions:

Don't use magic numbers. You have 67420 scattered throughout your code. If anything set that as a constant somewhere so it's obvious why you're using that and you don't mess up and write something like 670420 on accident.

Code:
NUMCOL <- 67420
at the top of your code would make this a lot nicer.

But better than just using 67420 you could grab that from the file itself if you can figure it out dynamically. That way if you have a file that is a different size your code still works.

Also your variables names are meaningless. If you give them more descriptive names it is easier to follow the code.

Did I mention your indentation isn't very good (no offense but indentation is really helpful for following code). Here is a nicer way to indent

Code:
## my code goes here
library(Matrix)
setwd("C:\\Users\\puppy\\Desktop\\img")
long <- file("C:\\Users\\puppy\\Desktop\\New folder (5)\\inra.bin", "rb")
lot <- file("C:\\Users\\puppy\\Desktop\\New folder (5)\\lat.img", "rb")

A <-readBin(long, integer(), size=2,n=67420*1, signed=F)
B <- readBin(lot, integer(), size=2,n=67420*1, signed=F)
ta<-t(A)
tb<-t(B)

listfile<-dir()

for (n in listfile){
    b <- file.info(n)$size/67420/4

    wind <- file(n, "rb")
    
    C <- readBin(wind, double(), size=4,n=67420*b, signed=TRUE)

    D<-matrix(C,nrow=b,ncol=67420)

    for(k in 1:b){
        M <- Matrix(-9999, 360, 720)
        tm <- t(M)
        
        for(i in 1:67420){
            tm[ta[i],tb[i]]= round(10 * mean(D[(k-1)*8 + 1:8), i])
        }

        to.write <- sprintf("C:\\Users\\puppy\\Desktop\\New folder (6)\\Yar_%00d.bin", k)
        writeBin(as.integer(tm@x), size=2,to.write)
    }
}
 
Last edited:

bryangoodrich

Probably A Mammal
#6
If you're going to transpose M immediately after you define M, why don't you just define it as the transpose? You don't use M as M anywhere else.
 

bryangoodrich

Probably A Mammal
#8
Have you tested it at the command line? I just did

Code:
k = 100
sprintf("C:\\Users\\puppy\\Desktop\\New folder (6)\\Yar_%00d.bin", k)
# [1] "C:\\Users\\puppy\\Desktop\\New folder (6)\\Yar_100.bin"
You should try putting in print statements in your code to show you the value of variables while you're developing your code. It's an easy (albeit, rough) way to debug some parts of your code.
 
Last edited by a moderator:

bryangoodrich

Probably A Mammal
#10
Why would you think assigning it 100 is appropriate? I was showing you what you get when you do that command. If you pass it '100' you get that output string. Either that is the sort of thing you intend to get or it isn't. I don't know what you're trying to do. That is what you're supposed to be figuring out.
 

Dason

Ambassador to the humans
#14
Good. Now 'puppy' objects to you. Do you know how hard it is to get a puppy to dislike you? Are you proud?