How to set the number of digits in a .txt file?

6 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2022 年 12 月 28 日
編集済み: Adam Danz 2022 年 12 月 28 日
Hello,
I have a question about readtable command. I use readtable command in order to read a .txt file. The problem is that The column in the .txt file has 6 numbers in each row, including zero as first numer of each row (e.g. 050305, 055625, 154625, 206478 etc.)
The proble is that after the readtable command use I observe that some numbers have 5 characters, "deleting" the zero of the first character (e.g. 50305, 55625, 154625, 206478 etc.).
Could you please help me in order to have 6 characters for all the numbers (as it is in the .txt file) ?

採用された回答

Adam Danz
Adam Danz 2022 年 12 月 28 日
編集済み: Adam Danz 2022 年 12 月 28 日
If you're using these values as numeric values, then you don't need the leading 0 since 056 is the same as 56.
If you're using these values as categorical values or labels then you can read them in as strings or categoricals. Or read them in as numbers and then convert them to string or categorical using:
T.Var = string(compose('%06.0f',T.Var))
% or
T.Var = categorical(compose('%06.0f',T.Var))
where T is your table and Var is the table variable name.
  2 件のコメント
Ivan Mich
Ivan Mich 2022 年 12 月 28 日
Ok, which command should I use in order to have the leading 0? is there a modifications of readtable command in order to make it?
Adam Danz
Adam Danz 2022 年 12 月 28 日
編集済み: Adam Danz 2022 年 12 月 28 日
You can use the T = readtable(filename,opts) syntax where opts is created by delimitedtextimportoptions. Set variableTypes to specify whether you're reading in the data as string or categorical.
It's probably easier to read the values in as numeric and then convert them using the example in my answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by