Efficient code for multiple string replace

7 ビュー (過去 30 日間)
Abeer Reza
Abeer Reza 2023 年 2 月 2 日
コメント済み: Voss 2023 年 2 月 2 日
I am attempting to replace substrings and evaluate equations for a large number of lines. The following code works for what I want:
% Define sets:
sec = ["T", "N"]; co = ["H","F"];
nco = numel(co); nsec = numel(sec);
for cc = 1:numel(co);
for ss = 1:numel(sec);
eval(replace(['tau_payr_#S_#C_bar = 1; '],["#S","#C"], [sec(ss),co(cc)]));
eval(replace(['tau_pinv_#S_#C_bar = 2; '],["#S","#C"], [sec(ss),co(cc)]));
eval(replace(['tau_payr_#S_#C = tau_payr_#S_#C_bar; '],["#S","#C"], [sec(ss),co(cc)]));
eval(replace(['tau_pinv_#S_#C = tau_pinv_#S_#C_bar; '],["#S","#C"], [sec(ss),co(cc)]));
I am wondering whether there's a better way to write this, where I don't have to write eval and replace at each line.


Voss 2023 年 2 月 2 日
You can store all those variables in a structure, and use dynamic field names.
sec = ["T", "N"]; co = ["H","F"];
nco = numel(co); nsec = numel(sec);
S = struct();
for cc = 1:numel(co)
for ss = 1:numel(sec)
payr_name = sprintf('tau_payr_%s_%s',sec(ss),co(cc));
pinv_name = sprintf('tau_pinv_%s_%s',sec(ss),co(cc));
S.([payr_name '_bar']) = 1;
S.([pinv_name '_bar']) = 2;
S.(payr_name) = S.([payr_name '_bar']);
S.(pinv_name) = S.([pinv_name '_bar']);
S = struct with fields:
tau_payr_T_H_bar: 1 tau_pinv_T_H_bar: 2 tau_payr_T_H: 1 tau_pinv_T_H: 2 tau_payr_N_H_bar: 1 tau_pinv_N_H_bar: 2 tau_payr_N_H: 1 tau_pinv_N_H: 2 tau_payr_T_F_bar: 1 tau_pinv_T_F_bar: 2 tau_payr_T_F: 1 tau_pinv_T_F: 2 tau_payr_N_F_bar: 1 tau_pinv_N_F_bar: 2 tau_payr_N_F: 1 tau_pinv_N_F: 2
  2 件のコメント
Voss 2023 年 2 月 2 日
You're welcome! Glad it helps.


その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 2 月 2 日
If you are going to write something like that, then you should write it in the clearest way you can come up with, because you are going to end up spending a lot of time staring at the code when it fails to work the way you expect.
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.
  1 件のコメント
Abeer Reza
Abeer Reza 2023 年 2 月 2 日
I totally agree with how difficult it is to read this blasted code. The issue is that for my purpose, the variable name is very important. I need a method to dynamically generate variable names with letter suffixes not as an interim calculation step or to use as a placeholder, but as the final product.
Although I totally agree with the points made in the link you shared (thanks for that, btw), saving calculated values in arrays or structures improve code efficiency unfortunately does not serve my current purpose.
I'm no fan of eval or the replace commands, but I am not sure what else can be used to generate the variable names as above.
The only other idea I have is to use the loops and the replace commands to generate a new .m file which can be run independently (not using eval).
Any other suggestions or advice on whether the last idea is any good?



Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by