Reading a textfile without removing leading zeros

조회 수: 8 (최근 30일)
Abhinav
Abhinav 2018년 2월 7일
댓글: Star Strider 2018년 2월 7일
I want to read the attached text-file which contains two columns: text and numeric. I used the code:
fileID=fopen('NWISMapperExport.txt','r');
z=textscan(fileID,'%s %f');
fclose(fileID);
The problem is that it removes leading zeros in the second column, is there is a simple way to retain zeros? I had the same problem with importdata.

채택된 답변

Star Strider
Star Strider 2018년 2월 7일
If you want to retain the leading zeros, you have to read them in as strings:
filename = 'NWISMapperExport.txt';
fidi = fopen(filename, 'rt');
D = textscan(fidi, '%s%s', 'CollectOutput',1);
Look = D{:}(1:5,2) % Check First Five Rows, Second Column
Look =
5×1 cell array
{'00152020'}
{'00500460'}
{'00501860'}
{'02380002'}
{'02380003'}
  댓글 수: 2
Abhinav
Abhinav 2018년 2월 7일
Thanks!
Star Strider
Star Strider 2018년 2월 7일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by