How to replace string but not function

Dear Community,
In a string like: 'sinh(x).*f.*n'
I want to replace the n with X_Model(1). Using strrep however I end up with
siX_Model(1)h.*f.*X_Model(1)
which means that the n in sinh was also found to be an n and replaced. How can I make it exclusiv, so only if it matches exactly the replacement does take place? In a Matlab Script using 'Find and Replace' I can make this happen when I click 'Match Case' and 'Whole Word', so I want to have this functionality in a function.
Thanks and best Regards Ravi

 採用された回答

Sean de Wolski
Sean de Wolski 2016 年 10 月 18 日
編集済み: Sean de Wolski 2016 年 10 月 18 日

0 投票

This can be done elegantly and safely with Symbolic Math Toolbox
syms x f n model(ix)
expr = sinh(x).*f.*n
subs(expr,n,model(1))

2 件のコメント

Ravi Goyal
Ravi Goyal 2016 年 10 月 19 日
Thanks Sean,
Would you know of any way not using the Symbolic Math Toolbox? Best Regards Ravi
Sean de Wolski
Sean de Wolski 2016 年 10 月 19 日
Not a safe way that is completely fool proof.

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

その他の回答 (1 件)

Jan
Jan 2016 年 10 月 19 日
編集済み: Jan 2016 年 10 月 19 日

0 投票

Str = 'sinh(x).*f.*n'
Str2 = regexprep(Str, '\bn\b'
I cannot test this currently.
[EDITED]
Str2 = regexprep(Str, '(?:^|\W)n(?:$|\W)', 'X_Model(1)')

3 件のコメント

Ravi Goyal
Ravi Goyal 2016 年 10 月 19 日
Hey Jan, I also thought about somehow using regexprep but cannot get it to work. Your solution sadly also does not work.
Jan
Jan 2016 年 10 月 19 日
編集済み: Jan 2016 年 10 月 19 日
@Ravi: See [EDITED] Please test this exhaustively, because I'm not a regexp hero.
Ravi Goyal
Ravi Goyal 2016 年 10 月 21 日
Thanks Jan,
It does the job for the n but then fails for the normal replacement of x in sinh(x) by getting rid of the brackets...I guess i gotta go somehow with the symbolic toolbox or just leave it a known bug.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2016 年 10 月 18 日

コメント済み:

2016 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by