I am writing a function for remote sensing purposes using matlab
the user will enter a folder containing 7 files into the program each file is a band of an image and the names of them is:
"b1.dat"
"b2.dat"
"b3.dat"
"b4.dat"
"b5.dat"
"b6.dat"
"b7.dat"
for example if 2 is entered as the argument of the function it will search in seven file names that are in the access and then will show b2.dat
how do you suggest me to write the code
You can use uigetfiles to select the directory and dir to get a list of the folders contents. Once you have the list, strfind will tell you a file contains a given number.
Or, using uigetdir:
dirName = uigetdir('C:\', 'select a directory');
contents = dir(dirName);
for c = contents
name = c.name;
if strfind(name,'3')
fileToOpen = name{1};
end
end
I used these two lines of codes:
folder = uigetdir('D:\','Select the folder containing bands')
filenames = dir(folder)
the first line returns the path to the folder as I expected:
folder =
D:\RS\911130 TM bands
but the second line not. I have 7 files in my folder and it returns a 9x1 struct
filenames =
9x1 struct array with fields:
name
date
bytes
isdir
datenum
for example the contents of the filenames(1,1) is:
Related
I am new or beginner to shell script.
How to search for a string containing "PRODUCT_VARIANT_NAMES" from a makefile > > present in specific path and store all the values assigned to it in an array
[array name is PRODUCT_VARIANT_ARRAY_LIST].
For example, say mmhp.mk is the make file present in some path device\pro\mmh4p.mk & it has content as shown below:
mmh4p.mk
1.flnslndddsa
2.fssd
3.dasdsa
4.dsadsa
5.PRODUCT_VARIANT_NAMES :=
6. mmh4p_fgn.SKT_KR
7. mmh4p_fgn.KT_KR
8. mmh4p_fgn.OPEN_KR
9. mmh4p-pr_fne.OPEN_EU_DS
10. mmh4p_fne.OPEN_EU_DS
11.bdn;lkndlak;ns;lndnlsnkl
12.dsland;lsandd
13.dlamsdlmasd
14.ndlbadnsad
15. ;asmdsamd,
How to write a script to grep all the values from
"PRODUCT_VARIANT_NAMES :=" & store it in an array
PRODUCT_VARIANT_ARRAY_LIST should contain the values
mmh4p_fgn.SKT_KR, mmh4p_fgn.KT_KR, mmh4p_fgn.OPEN_KR,
mmh4p-pr_fne.OPEN_EU_DS, mmh4p_fne.OPEN_EU_DS
Note: The string "PRODUCT_VARIANT_NAMES :=" can defined in single line as below
PRODUCT_VARIANT_NAMES := mmh4p_fgn.SKT_KR mmh4p_fgn.KT_KR mmh4p_fgn.OPEN_KR mmh4p-pr_fne.OPEN_EU_DS mmh4p_fne.OPEN_EU_DS
or it can be multiple lines ending with back slash as shown above.
I have a directory with many files, which are named based on a given pattern, for instance: User_TR1_ES-ES.csv, User_TR1_FR-FR.csv User_TR2_DE-DE.csv. The destination directory contains subfolders like these: folder_TR1, folder_TR2. I need to copy each files that contain TR1 in the basename within directory folder_TR1, and successively with the rest of the files. My code so far:
#I made an array with the list of files in original folder
file_list = Dir.children(output)
#I captured the parts of the file name that I'm interested in two variables
file_list.each do |file|
user_chars = file[5] + file[6] + file[7]
lang_chars = file[9] + file[10] + "-" + file[12] + file[13]
end
#Now I create a new path, in order to make the copy
original_path = File.join(output, "User_#{user_chars}_#{lang_chars}.csv")
new_path = #where I'm having issues
#in order to make the copy, I'd make the following
FileUtils.cp(original_path, new_path)
I just can't proceed on copying from one place to the desired folder, by following their filenames. Any hint?
So taking a path like this:
path = "/path/to/User_TR1_ES-ES.csv"
You want to extract TR1 from it, you can use
id = File.basename(path).split("_")[1]
Now id will equal "TR1". From here you want to copy it, so you can just supply the destination folder:
target_dir = "/path/to/folder_#{id}"
FileUtils.copy path, target_dir
Could someone please explain why local variables passed to the same nested function give different results.
The following function import_alignment() includes a conditional block if trimref: which changes the input data and calls the function again on this new data.
When I wrote this function it appeared to do the expected manipulations but returned the original data.
I've just realised that I neglected to assign results returned from the second call to the associated variables.
originally import_alignment(modified *args) now...
sequences, numseqs, refseq, refname = import_alignment(modified *args)
Which does what it should, but I'm still not sure why the first version didn't work.
...more details...
The function imports DNA sequence data from a Fasta file (a text file with >name and sequence on alternate lines), and returns list of sequences, along with the sequence count, and the name and sequence of a gap-free reference (the first sequence in the file).
It first calls the numseqs() function which does a basic quality check on the input file.
It then opens the file, assigns the odd lines to the sequences list (lines 0, 2, 4,... are sequence names; 1, 3, 5 are sequences), extracts the reference name and removes gaps ('-') from the reference sequence.
It then tests whether trimref == True if so then it harvests the sequence names from the file,
`trim_TOref() removes reference gap positions from all sequences
output_data() writes the resulting sequence alignment to a new fasta file (name_trimmed.fas)
this file is then passed back to import_alignment() to extract the modified sequences
def import_alignment(filename, path, trimref=True):
"""
this function extracts sequences from a fasta file and does basic QC
"""
infile = path + filename
filesize = file_size(infile)
print("Input path and filename : ",infile)
print("File Size : {0}".format(filesize))
numseqs = check_even(infile)
try:
with open(infile) as f:
f.seek(0)
sequences = pick_lines(f, odds=True)
refseq = sequences[0].replace("-","")
f.seek(0)
refname = f.readline().strip('>\n')
if trimref:
f.seek(0)
seqnames = pick_lines(f, False)
sequences = trim_TOref(sequences)
outfile = filename[0:-4] + "_trimmed"
output_data(outfile, sequences, outpath = (path + 'output/'), filetype = '.fas', keys = seqnames)
trimref = False
sequences, numseqs, refseq, refname = import_alignment((outfile + '.fas'), outpath, trimref)
except FileNotFoundError:
print("File not found. Check the path variable and filename")
exit()
return sequences, numseqs, refseq, refname
This is harder than I expected, but I have a folder with ~100 datasets in .csv format.
I would like to create a .csv file with the following fields:
The first field is the file's name. e.g. user_profile.csv
The second field is the file's absolute path, e.g. /Users/yuqli/project/user_profile.csv
I would like to do this with bash commands. But so far I have only be able to do :
ls >> out.csv
which will write all file names into a txt file... I see some people using a for loop, but manipulating lines in .csv file seems forbidding, and I don't know what to put inside the for loop...
Am I better off just using Python? Any help is appreciated... Thanks!
Thanks for the advice of gurus above, I came up with this Python program that 1) extracts file names and 2) extract field names in each file. Any comments are welcomed. Thanks!
import os
import csv
info = {} # store all information into a Python dictionary
for filename in os.listdir(os.getcwd()):
with open(filename, newline='') as f:
reader = csv.reader(f)
row1 = next(reader)
info[filename] = row1
path = os.getcwd()
header = 'field, dataset, path'
write_file = "output.csv"
with open(write_file, "w") as output:
output.write(header + '\n')
for key, value in info.items():
for elem in value:
curr_path = path + key
line = '{0}, {1}, {2}'.format(elem, key, curr_path)
output.write(line + '\n')
I have a set of images located in a folder and I'm trying to read these images and store their names in text file. Where the order of images is very important.
My code as follow:
imagefiles = dir('*jpg');
nfiles = length(imagefiles); % Number of files found
%*******************
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
The images stored in the folder in the following sequence : {1,2,3,4,100,110}.
The problem that Matlab read and write the sequence of images as { 1,100,110,2,3,4}. Which is not the correct order.
How can this be overcome?
I would suggest to use scanf to find the number of the file. For that you have to create a format spec which shows how your file name is built. If it is a number, followed by .jpg, that would be: '%d.jpg'.
You can call sscanf (scan string) on the name's of the files using cellfun:
imagefiles = dir('*jpg');
fileNo = cellfun(#(x)sscanf(x,'%d.jpg'),{imagefiles(:).name});
Then you sort fileNo, save the indexes of the sorted array and go through these indexes in the for-loop:
[~,ind] = sort(fileNo);
for ii=ind
currentfilename = imagefiles(ii).name;
% write the name in txt file
end