Matlab to R translation with system(sprintf())

Asked

Viewed 340 times

2

I am trying to translate the following Matlab code to R:

clear all
nsta = [1,2,3];
npx  = [2,3,4,5];
npu  = [2,3,4,5];
nmax = 2500;
nome = 'MODEL1b';
system(sprintf('del %s.log',nome));
nfiles = 0;
for k = 1:length(nsta)
    for i = 1:length(npx)
        for j = 1:length(npu)
            nx = nsta(k);
            npar = (npx(i)^nx)*npu(j);
            if (npar < nmax)
                nfiles = nfiles+1;
                system(sprintf('copy %s%d%d%d.txt  %s.txt',nome,nx,npx(i),npu(j),nome));
                system(sprintf('copy input%d%d%d.txt  input.txt',nx,npx(i),npu(j)));
                system(sprintf('copy template%d%d%d.txt template.txt',nx,npx(i),npu(j)));
                tic;
                system(sprintf('RFuzzy %s',nome));
                time = toc;
                fprintf('Arquivo: %d    Config: %d%d%d    Time: %f',nfiles,nx,npx(i),npu(j),toc);
                system(sprintf('copy min.txt min%d%d%d.txt',nx,npx(i),npu(j)));
            end
        end
    end
end

I tried the following code:

rm(list = ls())
nsta <- c(1,2,3)
npx <- c(2,3,4,5)
npu <- c(2,3,4,5)
nmax <- 2500
nome <- "MODEL1b"
system(sprintf('del %s.log', nome))
nfiles <- 0
for(k in 1:length(nsta)){
  for(i in 2:length(npx)){
    for(j in 2:length(npu)){
      nx <- nsta[k]
      npar <- (npx[i]^nx)*npu[j]
      if(npar < nmax){
        nfiles <- nfiles + 1
        system(sprintf('copy %s%d%d%d.txt  %s.txt',nome,nx,npx[i],npu[j],nome))
        system(sprintf('copy input%d%d%d.txt  input.txt',nx,npx[i],npu[j]))
        system(sprintf('copy template%d%d%d.txt template.txt',nx,npx[i],npu[j]))
        system(sprintf('RFuzzy %s',nome))
        fprintf('Arquivo: %d    Config: %d%d%d    Time: %f',nfiles,nx,npx[i],npu[j])
        system(sprintf('copy min.txt min%d%d%d.txt',nx,npx[i],npu[j]))
      }
    }
  }
}

On the seventh line, the error appears:

Warning message:
running command 'del MODEL1b.log' had status 127

How to proceed?

  • 1

    You are running R on Windows?

  • @Daniel , I’m.

  • 2

    Is the file directory the same directory that is running in the script? If necessary, use setwd("c:/nome_do_diretório"). See the default directory reference in Windows. Make sure this directory does not contain accent, it could generate some problem.

  • @Daniel I’m sure the directories are the same. Any more ideas?

  • Your script is crashing on the first line requiring iteration. The only problem associated with it is this. Run system("dir") and make sure you are recovering the log file.

  • Pq do not use file.remove() and file.copy()?

  • @Rcoster how so? May suggest a response?

Show 2 more comments

1 answer

0

Try:

rm(list = ls())
nsta <- c(1, 2, 3)
npx <- c(2, 3, 4, 5)
npu <- c(2, 3, 4, 5)
nmax <- 2500
nome <- "MODEL1b"
file.remove(sprintf('%s.log', nome))
nfiles <- 0

for(k in 1:length(nsta)){
     for(i in 2:length(npx)){
         for(j in 2:length(npu)){
         nx <- nsta[k]
         npar <- (npx[i]^nx)*npu[j]
         if(npar < nmax){
             nfiles <- nfiles + 1
             file.copy(from = sprintf('%s%d%d%d.txt', nome, nx, npx[i], npu[j]), to = sprintf('%s.txt', nome))
             file.copy(from = sprintf('input%d%d%d.txt', nx, npx[i], npu[j]), to = 'input.txt')
             file.copy(from = sprintf('template%d%d%d.txt', nx, npx[i], npu[j]), to = 'template.txt')
             system(sprintf('RFuzzy %s', nome)) # Provavelmente aqui vai dar erro. Mas pelo menos ganhamos algumas linhas :)
             cat(sprintf('Arquivo: %d Config: %d%d%d Time: %f', nfiles, nx, npx[i], npu[j])
             file.copy(from = 'min.txt', to = sprintf('min%d%d%d.txt', nx, npx[i], npu[j]))
             }
         }
     }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.