Load a txt file:

조회 수: 5 (최근 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