Reading a textfile without removing leading zeros

12 vues (au cours des 30 derniers jours)
Abhinav
Abhinav le 7 Fév 2018
Commenté : Star Strider le 7 Fév 2018
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.

Réponse acceptée

Star Strider
Star Strider le 7 Fév 2018
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 commentaires
Abhinav
Abhinav le 7 Fév 2018
Thanks!
Star Strider
Star Strider le 7 Fév 2018
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by