Load a txt file:

5 views (last 30 days)
Ionut  Anghel
Ionut Anghel on 29 Sep 2015
Edited: Stephen23 on 30 Sep 2015
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 30000x30. Any idea would be appreciated. Thank you
  2 Comments
Stephen23
Stephen23 on 29 Sep 2015
Edited: Stephen23 on 29 Sep 2015
If you upload the file we can create the exact code required for you. You can upload text files by clicking the paperclip button above the textbox, then clicking both the Choose file and Attach file buttons.
Ionut  Anghel
Ionut Anghel on 30 Sep 2015
Edited: Walter Roberson on 30 Sep 2015
Hello,
I'm sorry this is my fault. I the dimensions of the matrix in the begining (30000x30) but in reality the number of the lines and column is unknown. So what I did, I adapted an example from here to count the number of lines from the file and later conver to a matrix where I skip header and end of file.
Here is the code and in attachement an example of file:
clc;
clear all;
close all;
fid = fopen('myexamplefile.txt','rt');
%fid=fopen('bison_PI610.txt','r');
fseek(fid, 0, 'eof');
chunksize = ftell(fid);
fseek(fid, 0, 'bof');
ch = fread(fid, chunksize, '*uchar');
nol = sum(ch == sprintf('\n')); % number of lines
% newfile = textscan(fid,'%s', nol-4, 'HeaderLines',2);
% result = newfile{1};
datacell = textscan(fid,'%s',nol-4, 'HeaderLines', 2, 'CollectOutput', 1);
result = datacell{1};
fclose(fid);

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 29 Sep 2015
Edited: Stephen23 on 30 Sep 2015
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);

More Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2015
filerows = 30000;
filecols = 30;
fmt = repmat('%f', 1, filecols);
fid = fopen('myfile.txt', 'rt');
datacell = textscan(fid, fmt, filerows, 'HeaderLines', 3, 'CollectOutput', 1);
fclose(fid);
result = datacell{1};

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!