MATLAB: Read txt file into a matrix

fscanfheaderlinestextscan

Hi, I have attached a txt file which I want to read onto a matrix
I also have another file, which has six columns instead of two. I want to skip the first two lines with text and blank, and read the rest into a matrix A. I have tried a lot of different things, but nothing seems to work for me. Hope somebody can help.
Best Regards Jakob

Best Answer

Just use textscan, it only requires a few lines of code:
fid = fopen('counts_VS_disp_2.txt','rt');
C = textscan(fid, '%f%f%f', 'MultipleDelimsAsOne',true, 'Delimiter','[;', 'HeaderLines',2);
fclose(fid);
And we can view the output cell array in the command window:
>> C
C =
[6779x1 double] [6779x1 double] [6779x1 double]
>> C{2}(1:10)
ans =
0
0.0090
0.0180
0.0270
0.0350
0.0440
0.0530
0.0620
0.0710
0.0800
For the six-column file, if the format is otherwise the same, you just need to change the format string from '%f%f%f' to '%f%f%f%f%f%f', and it should work.
And of course if you want all of those values in one numeric array rather than a cell array, just use cell2mat:
>> A = cell2mat(C);
Related Question