フィルターのクリア

how to change decimal places after point

1 回表示 (過去 30 日間)
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2016 年 3 月 22 日
編集済み: Stephen23 2016 年 3 月 22 日
I have so many text files named
data_70.000_32.125
data_70.124_32.120
data_74.120_32.123
I have to make them
data_70.0_32.125
data_70.124_32.12
data_74.12_32.123
means where three zero after decimal make it one zero and remaining no zero.
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 22 日
編集済み: Stephen23 2016 年 3 月 22 日
Actually the names with consistent three decimal digits are better, because they:
  • are easier to scan when printed in a list.
  • are easier to parse (consistent formats are always better).
  • sort correctly into numeric order using a standard sort. Here is an example showing that your new format does not sort correctly:
>> old = {'data_70.000_32.125.csv'; 'data_70.124_32.120.csv'; 'data_70.120_32.123.csv'};
>> sort(old) % correct numeric order
ans =
'data_70.000_32.125.csv'
'data_70.120_32.123.csv'
'data_70.124_32.120.csv'
>> new = {'data_70.0_32.125.csv'; 'data_70.124_32.120.csv'; 'data_70.12_32.123.csv'};
>> sort(new) % your format gives the wrong numeric order.
ans =
'data_70.0_32.125.csv'
'data_70.124_32.120.csv'
'data_70.12_32.123.csv'
Of course you can then use my FEX submission natsortfiles to get them back into the correct order, but why bother when those trailing zeros do the job anyway.
I don't see any good reason to remove those zeros, just disadvantages.

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
v={'data_70.000_32.125';'data_70.124_32.120';'data_74.120_32.123'}
w=cell(size(v))
for k=1:numel(v)
a=regexp(v{k},'_','split')
a2=a{2}
a3=a{3}
b2=str2double(regexp(a2,'\d+','match'));
b3=str2double(regexp(a3,'\d+','match'));
w{k}=sprintf('data_%d.%d_%d.%d',[b2 b3]);
end
celldisp(w)
  2 件のコメント
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2016 年 3 月 22 日
If I have so many text files How can we get the names as v in this code.How can we read all that text files to get the name.some 2000 text files are there.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
編集済み: Azzi Abdelmalek 2016 年 3 月 22 日
You can use the dir command to get all your text files located somewhere
s=dir(fullfile('your_folder','*.txt'))
files={s.name}

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by