Replace a character in a Table

24 ビュー (過去 30 日間)
mary
mary 2023 年 4 月 5 日
コメント済み: Walter Roberson 2023 年 4 月 6 日
Hi,
I have a table and I wanted to replace the character E into D, e.g. 5.1155017E-03 -> 5.1155017D-03
How could I do this?

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 4 月 5 日
編集済み: Walter Roberson 2023 年 4 月 5 日
You can use https://www.mathworks.com/help/matlab/ref/matlab.mixin.customcompactdisplayprovider-class.html to customize the way that a table displays when you disp() or inside the variable browser.
This will not affect how the table is output to a file if you use writetable(), so if your real task is to write the data to file using D format, then the above will not help.
If your actual task is to write to a file, then you are probably better off using compose() of the table variables using %E formats, and then use regexprep() with a pattern of '(?=[\d\.])E' and a replacement of 'D' -- unless, that is, you just happen to have text items that are of the form digit or period followed by E ... if so then the detection pattern would have to be made more careful.
After replacement of the characters, you would then write the characters to file, possibly using writelines()
  2 件のコメント
mary
mary 2023 年 4 月 5 日
編集済み: mary 2023 年 4 月 5 日
Thanks @Walter Roberson for your reply. My objective is to write the data to file and indeed, I do use writetable().
Is there anyway to modify directly this part taken from here:
new_Table = varfun(@(x) num2str(x, ['%' sprintf('.%dE', n_decimal)]), Table);
to write the data using D format? I tried to change .%dE into .%dD, but it did not work.
Walter Roberson
Walter Roberson 2023 年 4 月 6 日
fmt = sprintf('%%.%dE', n_decimal);
formatted = strrep(compose(string(fmt, Table{:,:})), 'E', 'D');
new_Table = array2table(formatted, 'VariableNames', Table.Properties.VariableNames);

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by