How can I read in the integer data from an alphanumeric formatted text file?

조회 수: 4 (최근 30일)
John
John 2014년 4월 15일
댓글: John 2014년 4월 15일
I have a text file in the following format:
615835520110131262000000 000000 000000 000000 000000 000000 000000 000000 000000
615835520110726262000000 000000 000000 000000 000000 000000 000000 000000 000000
615835520110727262-99999M-99999M-99999M-99999M-99999M-99999M-99999M-99999M-99999M
615835520110201262000000 000000 000000 000000 000000 000000 000000 000000 000000
615835520110202262000000 000014 000011 000007 000009 000008 000019 000007 000000
615835520110203262000000 000000 000000 000000 000000 000000 000000 000000 000000
615835520110204262000000 000000 000000 000000 000000 000000 000000 000000 000000
I wish to convert it into an n x n vector (e.g. A), in the format above, however, the first column should not contain 61583552011XXX262. Also, for the the -99999 values, no M should be present, yet it sperates the vales. Any suggestions?
  댓글 수: 1
John
John 2014년 4월 15일
please let me know if more clarification is needed! any help is much appreciated

댓글을 달려면 로그인하십시오.

채택된 답변

Sara
Sara 2014년 4월 15일
fid = fopen('myfile.txt','r');
n = 18; % characters that you want to eliminate 61583552011XXX262
nrow = 100; % number of rows in the file or a number larger that the expected # of lines
ncol = 9; % I assume you know how many data you have
A = zeros(nrow,ncol);
for i = 1:nrow
t = fgetl(fid);
if(~ischar(t)),break,end %end of file
t = t(n+1:end); %only the part that you want
% Remove the M
k = strfind(t,'M');
t(k) = blanks(1);
A(i,:) = str2num(t);
end
A = A(1:i,:);
fclose(fid);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by