How do I treat space in a text file as leading zeros?
1 回表示 (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2020 年 3 月 11 日
回答済み: MathWorks Support Team
2020 年 4 月 6 日
I am reading in numbers from a text file to a string. For example, consider the string below, which contains 4 integers: 99919, 99920, 100000, 99997:
>> str=' 99919 99920100000 99997'
Each takes up exactly 6 characters in the string, with leading zeros shown as spaces. I am trying to use "textscan" to read these numbers one-by-one,
>> t = textscanf(str,'%6d')
>> celldisp(t)
but it is not working as expected.
採用された回答
MathWorks Support Team
2020 年 3 月 11 日
The "textscan" function cannot interpret spaces as numbers, so the best solution would be to convert all spaces to leading zeros.
To start, read the contents of your file into a single character vector, using "fileread". Convert all spaces in the character vector to leading zeros. Then, use "textscan" as before. For example, using the attached text file:
>> str = fileread('t.txt');
>> str(str == ' ') = '0';
>> t =textscan(str,'%6d');
>> celldisp(t)
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!