0
votes

I'm a newbie to MATLAB. I've a .txt file with 2 header lines and a total of 140000 rows and 6 columns. I want to extract only the first 116959 rows with any one column data of interest and store it as an vector. I want to use this column vector further for fft analysis. Like this I want to read multiple columns and store them as vectors.

Time        T_hor   T_ver   V_hor   V_ver   SPEED   
s       um  um  um  um  Hz  
39,000000   8,833   -15,43  -11,871 23,604  701,17  
39,000200   3,121   -22,78  -9,949  41,712  701,17  
39,000400   -8,012  -26,28  -4,317  33,790  701,17  
39,000600   -13,092 -20,22  8,343   20,630  701,17  
39,000800   -16,408 -5,27   6,869   5,680   701,17  
39,001000   -10,591 5,36    1,895   -0,005  701,17  
39,001200   2,016   -0,01   -7,054  6,786   701,17  
39,001400   8,622   -14,06  -11,581 20,998  701,17  
39,001600   4,279   -22,17  -10,002 39,791  701,17  
39,001800   -7,117  -25,70  -5,738  35,106  701,17  
39,002000   -12,697 -20,99  6,948   22,314  701,17  
39,002200   -16,355 -6,83   6,738   7,602   701,17  
39,002400   -11,960 5,68    2,079   0,469   701,17  
39,002600   0,463   2,10    -6,422  5,759   701,17  
39,002800   8,964   -11,91  -11,765 19,498  701,17  

This is how the data looks like ( ignore commas(,) the data is in the German format which is equivalent to '39.0012'). I tried using importdata with space delimiters. But it gave me errors. The following is the code I used.

filename = 'Test_Data.txt';
delimiterIn = ' ';
headerlinesIn = 2;
A = importdata(filename,delimiterIn,headerlinesIn);   

for k = [3, 5] % Extracting 3rd and 5th column
   disp(A.colheaders{1, k})
   disp(A.data(:, k))
   disp(' ')
end

I get errors when using this method.I guess there is something more easy and logical I'm missing. Could someone shed some light on this.

1
A similar problem with dot and comma was discussed here: stackoverflow.com/questions/8204080/… - Daniel
Thanks for your comment. I checked that code. Apart from the conversion. My question was "How to read and store as a single column vector" - Agni
fread would be the most efficient, but importdata is probably the easiest way to load and later to transform your data. It is however a little bit slow. - C.Colden
@c.colden when I use fread it returns me a value '0' - Agni
I apologize, I meant fscanf - fread is only for binary files. Sorry once again. Please check ch.mathworks.com/help/matlab/ref/fscanf.html for more information - C.Colden

1 Answers

0
votes

If possible I would suggest you the following: Get rid of the spaces and replace them with commas and replace your commas with dots. Load this file as you did using importdata or alternatively fread or dlmread. Now you can access the data very easy as you showed it in your example.