0

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.

Community
  • 1
  • 1
Agni
  • 125
  • 2
  • 15
  • 1
    A similar problem with dot and comma was discussed here: http://stackoverflow.com/questions/8204080/matlab-how-to-read-in-numbers-with-a-comma-as-decimal-separator – Daniel Jan 19 '15 at 11:33
  • 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 Jan 19 '15 at 13:01
  • `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 Jan 19 '15 at 14:22
  • @c.colden when I use fread it returns me a value '0' – Agni Jan 19 '15 at 14:49
  • I apologize, I meant `fscanf` - `fread` is only for binary files. Sorry once again. Please check http://ch.mathworks.com/help/matlab/ref/fscanf.html for more information – C.Colden Jan 19 '15 at 15:40
  • @c.Colden Thanks for your reply. From the link you provided. I've a question. How do we read each column. I meant how to implement delimiter to extract only the column I need and store it in an vector. – Agni Jan 19 '15 at 16:01
  • 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` and now you can access the data very easy like you showed it in you example! Alternative also `dlmread`would work, too. – C.Colden Jan 20 '15 at 12:56
  • @C.Colden. I replaced commas with dots and imported the file using fopen. Now it works. However I'd issues with other functions. – Agni Jan 22 '15 at 10:23
  • If the error is something related, let me know. I will add my answer, if you don't mind accept it. – C.Colden Jan 22 '15 at 14:51

1 Answers1

0

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.

C.Colden
  • 627
  • 1
  • 8
  • 28