FSL_Force <- function(Sbot,Stop,CS,nsarc) { ##set here the folder where the files are placed ##setwd("/Users/felipedesouzaleite/Documents/McGill_Courses/Coursera/Intro to R programming/Files/Functions") ## isolating and offseting X displacement of the needles BOT <- read.table("BOT.xls", header=TRUE) BOTdisp <- c(BOT[,"POSITION_X"]) x <- c(BOTdisp - BOTdisp[1])*-1 TOP <- read.table("TOP.xls", header=TRUE) TOPdisp <- c(TOP[,"POSITION_X"]) y <- c(TOPdisp - TOPdisp[1]) ## check the length of both needles to see if they have the same size. print(length(x)) print(length(y)) if(length(x) != length(y)) print("Different Lengths") if(length(x) == length(y)) print("Matching Lengths") ## calculate the force w <- c(x+y) z <- c(w*(Sbot*Stop)/(Sbot+Stop)) force <- c(z/(pi*((CS/2)^2)))*-1 copyforce <<- force #baseline <- mean(force[1:150]) #peak1 <- max(force[1:Cut]) #peak2 <- max(force[Cut:length(force)]) #Force1 <- peak1-baseline #Force2 <- peak2-baseline #avgForce <- (Force1+Force2)/2 #print(Force1) #print(Force2) #print(avgForce) ## this to estimate the average sarcomere length SarcLength <- (c(TOPdisp-BOTdisp))/nsarc copysarclength <<- SarcLength ##Plotting par(mfrow=c(2,1)) plot(force,type = "o", main = "Force vs Frame", ylab = "nN/um^2", xlab = "Frame",col = "red") axis(side=1, at=seq(0,length(force),by=100)) plot(SarcLength, main = "SL vs Frame", ylab = "um",xlab = "Frame") axis(side=1, at=seq(0,length(SarcLength),by=100)) ## Creates a output file ##time <- (c(1:length(force)))*(1/43.3) ##data <- data.frame(time,force, SarcLength,Force1,Force2,avgForce) ##write.table(data,file = "OutputFromR.csv", sep=",", qmethod = "double",col.name=NA, row.name=TRUE) } FSL_finaldata <- function(peak1,baseline2,peak2){ ##Returns the first baseline baseline <- mean(copyforce[1:150]) #print(baseline) ##Finds the first peak average x <- c(copyforce[1:peak1]) y <- c(copyforce[peak1:length(copyforce)]) w <- c(tail(x,80),head(y,80)) firstpeak <- mean(w) print(firstpeak) ##Finds the middle baseline average x <- c(copyforce[1:baseline2]) y <- c(copyforce[baseline2:length(copyforce)]) w <- c(tail(x,43),head(y,43)) middlebaseline <- mean(w) print(middlebaseline) ##Finds the second peak average x <- c(copyforce[1:peak2]) y <- c(copyforce[peak2:length(copyforce)]) w <- c(tail(x,80),head(y,80)) secondpeak <- mean(w) print(secondpeak) ##Finds the final baseline average. Does not require a variable x <- c(tail(copyforce,80)) finalbaseline <- mean(x) print(finalbaseline) ##Returns the forces Force1 <- firstpeak-((baseline+middlebaseline)/2) Force2 <- secondpeak-((middlebaseline+finalbaseline)/2) AvgForce <- ((Force1+Force2)/2) print(Force1) print(Force2) print(AvgForce) ## Creates an output file time <- (c(1:length(copyforce)))*(1/43.3) data <- data.frame(time,copyforce, copysarclength,Force1,Force2,AvgForce) write.table(data,file = "Output.csv", sep=",", qmethod = "double",col.name=NA, row.name=TRUE) }