Leader

Wednesday 26 November 2008

How to: Importing data

Final code:

Filelist = dir('*.txt')
for i = 1:length(Filelist)
     Filename = Filelist(i).name
     Tempdata = dlmread(Filename)
     Data(:,i) = Tempdata(:,2)
end


Explanation
Importing raw data is the first step in data analysis. If done properly (ie. programmatically) it can be automated, which is much less hassle than using the import tool to do each file one at a time.


MATLAB has a number of functions designed to import data in different forms, including ‘CSVREAD’, ‘DLMREAD’, ‘FSCANF’, ‘LOAD’, ‘TEXTREAD’ and ‘TEXTSCAN’. We’ll be using DLMREAD in the example, which imports ASCII numeric data and allows you to specify a delimiter (the character that separates columns of data), if needs be. Check out the help files for how to use the others - the general way of implementing them in a script is usually the same.

Generally the importing functions work in the same way; the inputs include a filename and specific options, and the output is the data, which can be saved into a suitable variable.



AdSense