MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

How to remove numbers between specific symbols in text array?

Joseba Moreno さんによって質問されました 2019 年 2 月 14 日
最新アクティビティ madhan ravi
さんによって 編集されました 2019 年 2 月 14 日
Hello everyone,
I have a question regarding how to modify a text array in Matlab which contains numbers between two "_" symbols. Here some examples:
  • If the string has the following structure: "Gas_1_O2" I expect the following result: "Gas_O2". This should work for every number between two "_" symbols.
  • If the string has another structure nothing should happen (e.g. "Time" or "Flow_Fluidization").
The text string will later on be used for renaming a table.
I tried with the function extractBetween but it did not worked as I expected.
Thanks for your support.
Best,
Joseba

  0 件のコメント

サインイン to comment.

3 件の回答

回答者: madhan ravi
2019 年 2 月 14 日
 採用された回答

a="Gas_1_O2";
regexprep(a,'\_\d*.?\d*\_','_')

  5 件のコメント

madhan ravi
2019 年 2 月 14 日
In that case:
regexprep(a,'\_(\d+[\.]?\d*)\_','_')
"The code was put keeping only numbers (including decimals)..."
Remember that . by itself matches all characters, not just the decimal point / period chraacter.
madhan ravi
2019 年 2 月 14 日
True Stephen that‘s why I used [\.] to preserve dot character in the latter comment.

サインイン to comment.


回答者: Rik
2019 年 2 月 14 日

You can use regular expressions for this task:
str1='Gas_1_O2';
str2='Flow_Fluidization';
regexprep(str1,'_[0-9]*_','_')
regexprep(str2,'_[0-9]*_','_')

  0 件のコメント

サインイン to comment.


回答者: Joseba Moreno 2019 年 2 月 14 日

Hello again,
the script works good, thanks for it. I am facing now a minor problem with "duplicate variable names", due to the following reason:
Before renaming: "Temp_01_free", "Temp_02_free", etc.
After renaming: "Temp_free" in all cases. Is it possible to number the variables then as long as they are repeated? For instance: "Temp_free1", "Temp_free2", etc.
Thanks again!
Joseba

  1 件のコメント

Rik
2019 年 2 月 14 日
Why are you numbering variable names? That's a bad idea.

サインイン to comment.



Translated by