Load a txt file:

1 次查看(过去 30 天)
Ionut  Anghel
Ionut Anghel 2015-9-29
编辑: Stephen23 2015-9-30
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 个评论
Stephen23
Stephen23 2015-9-29
编辑:Stephen23 2015-9-29
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 2015-9-30
编辑:Walter Roberson 2015-9-30
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);

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-9-29
编辑:Stephen23 2015-9-30
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);

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-9-29
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};

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by