Sorting files in Matlab -


i have 50 files full of lines of data. i'm trying have program can open files, read line 8 of files sort files according line 8. line 8 represents longitude.

so files of lowest longitude comes first. i'm trying in vain fget function , think impossible. posted before deleted because made mistake in tagging.

an example of file looks shown below:

    1 cruise_number: 2006002     2 cruise_name: arcticnet 0602     3 original_filename: ctd_2006002_016_1_dn.odf     4 station : station ba04     5 cast_number  : 016     6 start_date_time [utc]: 07-sep-2006 02:05:00.00     7 initial_latitude [deg]: 75.277     8 initial_longitude [deg]: -74.9482     9 sounding [m]: 489    10 min_depth [m]: 7.27    11 max_depth [m]: 462.57 

first things first, should use dir , fullfile read of files in folder / directory. output of dir structure of information represents each file in directory. reason why use fullfile because path separator between folders / directories different on every operating system. on linux/mac, it's / while on windows it's \. in order operating system agnostic, let fullfile build path of directory you.

after, use for loop iterate on each file, open up, processing , close file. i'd use fgetl when open file, , call 8 times 8th line. fgetl read in line string. next, number @ end of line using regular expressions, convert number represented string actual number, place array.

in end, you'll have 50 numbers, sort these numbers , corresponding sorted indices, reorder output dir.

i'm going assume of files placed inside single directory. also, assuming of files have extension of .txt , this:

folder = fullfile('path', 'to', 'folder'); %// replace files here files = dir(fullfile(folder, '*.txt')); %// find files in folder  longitude = zeros(numel(files), 1); %// initialize longitude array  idx = 1 : numel(files) %// each file     fileid = fopen(fullfile(folder, files(idx).name)); %// open file      idx2 = 1 : 8         str = fgetl(fileid); %// skip 8th line     end      %// extract out number @ end of string     numcell = regexp(str, '-?\d+\.\d+$', 'match');      %// place array     longitude(idx) = str2double(numcell{1});      %// close file     fclose(fileid); end  %// sort longitudes , index ordering [~,ind] = sort(longitude);  %// reorder files in structure files = files(ind); 

let's step through code line line. first line specify files located. each subfolder separated single string, keep in mind when modifying first line of code. second line finds list of files have .txt extension in said directory. keep in mind filenames with respect input directory of dir if still want access file, you'll need use fullfile again. next, create array store our longitudes read in each file.

next, loop through every text file in directory, open file fopen, use fgetl 8 times , skip 7 lines. 8th line contains text of interest. line, use regular expressions extract out number @ end of string. regular expressions mechanisms used find patterns in text. in case, want find specific pattern - namely floating point number possibly has negative value in front. use function regexp search pattern. first input string , second input pattern want choose. pattern looking rather cryptic @ first sight:

-?\d+\.\d+$ 

this says looking number may optionally have negative sign (-?) followed sequence of 1 or more digits (\d+), followed decimal point (\.), followed sequence of digits (\d+), , make sure happens at end of string ($). use flag 'match' return actual matched strings. not doing return locations of strings found, , that's not want. returned cell array of strings, , if done correctly, there should 1 element in returned cell array. access element, convert actual number via str2double, log our longitude array.

when we're done looping, sort these longitudes , extract index ordering. use index ordering reorder names of files structure returned dir complete code. f contain filenames in sorted order reference longitude. i'm not sure you'd want after, i'll leave answer is.

however, if want see list of of files in sorted order, can do:

filessorted = char(files.name); 

this unpack of names of filenames , place them 2d character array each row filename. first row filename smallest longitude, second row filename second smallest longitude , on.

you can place of names in cell array by:

filessorted = {files.name}; 

then access particular filename, do:

file = filessorted{idx}; 

idx number 1 many files have.


good luck!


Comments