Efficient code for multiple string replace

6 visualizaciones (últimos 30 días)
Abeer Reza
Abeer Reza el 2 de Feb. de 2023
Comentada: Voss el 2 de Feb. de 2023
Hello,
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)]));
end;
end;
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.
Thanks,

Respuesta aceptada

Voss
Voss el 2 de Feb. de 2023
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']);
end
end
S
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 comentarios
Abeer Reza
Abeer Reza el 2 de Feb. de 2023
okay, this is brilliant!! I guess I didn't understand some of the points in the link shared earlier.
Thanks :-)
Voss
Voss el 2 de Feb. de 2023
You're welcome! Glad it helps.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 2 de Feb. de 2023
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 comentario
Abeer Reza
Abeer Reza el 2 de Feb. de 2023
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by