MATLAB: Load a txt file:

dlmreadloadtext file

Hi, I have the following question: Supose I have a txt file, myfile.txt and it look like this:
*/header
fff.235
T 531VV951 211VV015L1 211VV035 211VV101
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
T
531VV951
211VV015L1
211VV035
211VV101 */
I want to know how can I skip first 3 lines, i.e
/header;fff.235;T 531VV951...
and the last 5 lines (i.e T; 531VV951; ) I was deleting manually those lines and used after AA=load('myfile.txt') and it was working fine. Alternatively I used data= dlmread(myfile.txt, ',',3 ,); but I don't know how should I do it for the last 5 lines which I have to skip. There are now 50 files and matrix is 30000×30. Any idea would be appreciated. Thank you

Best Answer

You could use the function textscan, which lets you select the headerlines to ignore (so you don't need to delete any lines by hand), and also define the number of rows that should be read into MATLAB.
EDIT to match newly uploaded textfile.
This code reads that text file, including the header, the matrix, and the trailing data. It also adjusts automatically to the number of columns.
fid = fopen('myexamplefile.txt','rt');
line1 = fgetl(fid);
hdr = regexp(fgetl(fid),'\S+','match');
fmt = repmat('%f',1,numel(hdr));
C = textscan(fid,fmt);
D = textscan(fid,'%s%s%s%s',1,'HeaderLines',1);
fgetl(fid);
E = textscan(fid,'%s%f%f%f%f',1,'HeaderLines',1);
fclose(fid);