Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through files in a folder in matlab

I have a set of days of log files that I need to parse and look at in matlab.

The log files look like this:

LOG_20120509_120002_002.csv (year)(month)(day)_(hour)(minute)(second)_(log part number) 

The logs increment hourly, but sometimes the seconds are one or two seconds off (per hour) which means i need to ignore what they say to do loadcsv.

I also have another file:

LOG_DATA_20120509_120002.csv 

which contains data for the whole hour (different data).

The overall objective is to:

 loop through each day       loop through each hour          read in LOG_DATA for whole hour          loop through each segment              read in LOG for each segment                  compile a table of all the data 

I guess the question is then, how do i ignore the minutes of the day if they are different? I suspect it will be by looping through all the files in the folder, in which case how do i do that?

like image 888
Fantastic Mr Fox Avatar asked Jul 23 '12 23:07

Fantastic Mr Fox


People also ask

How do you process a file using a loop?

Create a tuple having the extensions that you want to fetch. Through a loop iterate over all the files in the directory and print the file having a particular extension. The endswith() function checks if the file ends that particular extension or not is its does then it prints the file name.

How do you access files in a folder in MATLAB?

On the Home tab, in the File section, click Open , and then select a file to open. You also can double-click the file in the Current Folder browser.


2 Answers

Looping through all the files in the folder is relatively easy:

files = dir('*.csv'); for file = files'     csv = load(file.name);     % Do some stuff end 
like image 175
Isaac Avatar answered Sep 21 '22 08:09

Isaac


At first, you must specify your path, the path that your *.csv files are in there

path = 'f:\project\dataset' 

You can change it based on your system.

then,

use dir function :

files = dir (strcat(path,'\*.csv'))  L = length (files);  for i=1:L    image{i}=csvread(strcat(path,'\',file(i).name));       % process the image in here end 

pwd also can be used.

like image 40
PyMatFlow Avatar answered Sep 24 '22 08:09

PyMatFlow