Zunqiu Chen
2011-Jan-28 23:23 UTC
[R] any similiar R fuction for matlab function 'fprintf'?
Dear All, Currently, I am translating Matlab code to R. I met difficulties to translate such Matlab codes into R: fprintf(fid,(' SPLITTING RESULTS ')); fprintf(fid,(' \n')); fprintf(fid,' Data base to analyze is a matrix %4i x %2i ',[n m]); fprintf(fid,' \n'); fprintf(fid,' h is %2i',h); fprintf(fid,' \n'); fprintf(fid,' group=0 outlier.\n'); fprintf(fid,' group=-1 discriminator or observation removed by the rules.\n'); fprintf(fid,' \n'); Does anyone happen to know if R has similar function as fprintf in Matlab? Thanks a lot! Best Zunqiu [[alternative HTML version deleted]]
David Winsemius
2011-Jan-28 23:30 UTC
[R] any similiar R fuction for matlab function 'fprintf'?
On Jan 28, 2011, at 6:23 PM, Zunqiu Chen wrote:> Dear All, > > Currently, I am translating Matlab code to R. I met difficulties to > translate such Matlab codes into R: > fprintf(fid,(' SPLITTING RESULTS ')); > fprintf(fid,(' \n')); > fprintf(fid,' Data base to analyze is a matrix %4i x %2i ',[n m]); > fprintf(fid,' \n'); > fprintf(fid,' h is %2i',h); > fprintf(fid,' \n'); > fprintf(fid,' group=0 outlier.\n'); > fprintf(fid,' group=-1 discriminator or observation removed by the > rules.\n'); > fprintf(fid,' \n'); > > Does anyone happen to know if R has similar function as fprintf in > Matlab?Don't know any Matlab, but you should look at: ?cat ?sprintf ?format ?formatC And in the future, why not provide an English description of what you are hoping for? Also may want to consult: http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf -- David Winsemius, MD West Hartford, CT
There does not seem to be a function exactly similar to MATLAB's "fprintf" in R, but you can achieve pretty much the same results using something like the code below: a <- 1:10 b <- 11:20 ab <- cbind(a,b) sink("C:\\R\\test.txt") # open file to write data to - file will be deleted if it exists cat(paste("The number is: ", 5, "\n", sep="")) sprintf("The dimensions of matrix ab is: %i x %i", dim(ab)[1], dim(ab)[2]) # compare with the line below cat(sprintf("The dimensions of matrix ab is: %i x %i", dim(ab)[1], dim(ab)[2]),"\n") # compare with the line above cat(sprintf("%s is %f feet tall", "Sven", 7.1),"\n") sink() # close file # do more stuff # append to previous file sink("C:\\R\\test.txt", append=TRUE) # open file for appending cat("\n") cat("Test in appending\n") sink() # close file You wrote: Dear All, Currently, I am translating Matlab code to R. I met difficulties to translate such Matlab codes into R: fprintf(fid,(' SPLITTING RESULTS ')); fprintf(fid,(' \n')); fprintf(fid,' Data base to analyze is a matrix %4i x %2i ',[n m]); fprintf(fid,' \n'); fprintf(fid,' h is %2i',h); fprintf(fid,' \n'); fprintf(fid,' group=0 outlier.\n'); fprintf(fid,' group=-1 discriminator or observation removed by the rules.\n'); fprintf(fid,' \n'); Does anyone happen to know if R has similar function as fprintf in Matlab? Thanks a lot! Best Zunqiu Jude Ryan Director | MarketShare 1270 Avenue of the Americas, Suite # 2702, New York, NY 10020 P: 646.745.9916 x222 | M: 973.943.2029 www.marketshare.com twitter.com/marketshareco This email message is for the sole use of the intended r...{{dropped:10}}
Zunqiu Chen
2011-Feb-03 23:28 UTC
[R] any similiar R fuction for matlab function 'fprintf'?
Thanks a lot! It really helps. Zunqiu From: Jude Ryan [mailto:jryan@marketshare.com] Sent: Thursday, February 03, 2011 3:28 PM To: Zunqiu Chen Cc: R-help@r-project.org Subject: Re: [R] any similiar R fuction for matlab function 'fprintf'? There does not seem to be a function exactly similar to MATLAB’s “fprintf” in R, but you can achieve pretty much the same results using something like the code below: a <- 1:10 b <- 11:20 ab <- cbind(a,b) sink("C:\\R\\test.txt") # open file to write data to - file will be deleted if it exists cat(paste("The number is: ", 5, "\n", sep="")) sprintf("The dimensions of matrix ab is: %i x %i", dim(ab)[1], dim(ab)[2]) # compare with the line below cat(sprintf("The dimensions of matrix ab is: %i x %i", dim(ab)[1], dim(ab)[2]),"\n") # compare with the line above cat(sprintf("%s is %f feet tall", "Sven", 7.1),"\n") sink() # close file # do more stuff # append to previous file sink("C:\\R\\test.txt", append=TRUE) # open file for appending cat("\n") cat("Test in appending\n") sink() # close file You wrote: Dear All, Currently, I am translating Matlab code to R. I met difficulties to translate such Matlab codes into R: fprintf(fid,(' SPLITTING RESULTS ')); fprintf(fid,(' \n')); fprintf(fid,' Data base to analyze is a matrix %4i x %2i ',[n m]); fprintf(fid,' \n'); fprintf(fid,' h is %2i',h); fprintf(fid,' \n'); fprintf(fid,' group=0 outlier.\n'); fprintf(fid,' group=-1 discriminator or observation removed by the rules.\n'); fprintf(fid,' \n'); Does anyone happen to know if R has similar function as fprintf in Matlab? Thanks a lot! Best Zunqiu Jude Ryan Director | MarketShare 1270 Avenue of the Americas, Suite # 2702, New York, NY 10020 P: 646.745.9916 x222 | M: 973.943.2029 www.marketshare.com twitter.com/marketshareco This email message is for the sole use of the intended r...{{dropped:10}}