Open up and load multiple files in Scilab with the extension .csv - scilab

I need a way to open all the files in a folder, regardless of the filename. All the files have a .csv extension.

You could use listfiles and csvRead like this
function doSomethingWithCSVdata(CSV_data)
disp(CSV_data);
endfunction
csv_files = listfiles('*.csv');
for i=1:size(csv_files,1)
disp('Opening file: '+csv_files(i))
csvData = csvRead(csv_files(i),ascii(9), [], 'string');
doSomethingWithCSVdata(csvData);
end

Related

Copy/move files by folder name pattern in R

I have a folder (~/PATH/MYFOLDER) with a lot of subfolders and files.
Subfolders are named, for example, as: LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq
Inside each subfolder has a lot of image files (*.png and/or *.jpg)
In ~/PATH/ I have folders with part of the name of subfolders, as: orgx, orgy, phyw, detz, appq
I would to copy image files of subfolders: LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq, to respective folders: orgx, orgy, phyw, detz, appq
Any help would be great.
Thanks all.
You can use sub to remove "MYFOLDER/Lclass_" from the file names. Something like this:
from = list.files(
path = "~/PATH/MYFOLDER",
pattern = "(png|jpg)$",
recursive = TRUE,
full.names = TRUE
)
to = sub(x = from, pattern = "MYFOLDER/Lclass_", replacement = "", fixed = TRUE)
file.copy(from = from, to = to)
This should take input from list.files like "~/PATH/MYFOLDER/LClass_orgx/file.jpg" (from) and change it to "~/PATH/orgx/file.jpg" (to), and then copy it accordingly. You could then use file.remove to delete the old ones. (Potentially you could do this all at once with file.rename, but it seems safer to copy and take a minute to check that things look right before deleting the old ones.)
If you need to be more specific in the sources, you could modify the list.files(pattern) to specify the source directories you mention, LClass_orgx, LClass_orgy, LClass_phyw, LClass_detz, LClass_appq.

Running multiple files from with Scilab program

I'm new to Scilab. I have to run the same program with a dozen different input files. Currently I simply uncomment the line and then rerun the program, and change the output file to a new name
// Input data file
data_file = 'data1.txt';
//data_file = 'data2.txt';
//data_file = 'data3.txt';
//data_file = 'data4.txt';
//data_file = 'data5.txt';
//data_file = 'data6.txt';
etc. another 6 lines
// Output data file name
output_data = '/output_files/data1.csv';
Is there a way to read in each file (data1.txt, data2.txt...) execute the body of the program and then output a new output file (data1.csv, data2.csv ...) instead of what I'm doing now, which is running the program and then editing it to use the next file and run again?
Just do something like:
for i=1:6
// Input data file
data_file=msprintf("data%d.txt",i);
// Output data file name
output_data=msprintf("/output_files/data%d.csv",i);
// exec the body of your script
end

How to save the R output in different directory?

I have three different folders name as "5k", "10k" and "15k", I can save the R out from the following code using this for loop.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
setwd(paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i]))
save(y, file =paste0("ydat",iter_name[i],".RData"))
}
Is there any shortcut or better way to do this.
Any help is appreciated.
Try the following code. It looks like you are omitting / in the second call to paste0.
iter_no=c(5000,10000,15000)
iter_name=c("5k","10k","15k")
for ( i in 1:length(iter_no)){
y=rnorm(iter_no[i])
file = paste0("C:/Users/Owner/Desktop/prac_fol/",iter_name[i], '/' , "ydat",iter_name[i],".RData")
save(y, file = file)
}

How can i fetch starting of file name from the path with different extensions using R

"/D/data_DataAnalysis/Progrm/datset1/set2/genus/Huttenhower_LC8_genus_reported.tsv"
"/c/bioinfoTools/data/mock/test/truth/file_sets/genus/Huttenhower_LC8_TRUTH.txt"
I want "Huttenhower_LC8" from two file name using R.
Similer to the python code
fileName_temp = a_file.split("/")[-1]
filename = a_file.split("/")[-1][:-9]
for another_file in all_slim_files:
a_filename = another_file.split("/")[-1][:-18]

Deleting text from .txt file from within R

I have some code which appends text over an existing .txt file , from within R as following :
write("puts 'hellow world '", file = "C:\\Ruby22-x64\\bin\\elt.rb",
append = TRUE, sep = " ")
setwd("C:/Ruby22-x64/bin/")
test<-system("ruby elt.rb",intern=TRUE) # this will return "hellow world" from ruby interpreter
My question is: after appending the .rb file and running it, how can i remove the "puts 'hellow world '" string from the .rb file, and return it to its initial state?
I tried to look for many functions, but couldn't find any function which can undo the write function.
Just found out a wonderful gist that does the Job : https://gist.github.com/mages/1544009
gist:1544009

Resources