Error in using eval with num2str
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Farid Khazaeli Moghadam
am 24 Aug. 2022
Bearbeitet: Stephen23
am 24 Aug. 2022
I want to rename a variable within the loop by using matlab standard eval and num2str functions.
Here is the line of the code I've written to do so:
eval(['DTU_10MW_RWT_s' num2str(seed) '_' num2str(wind) '_' num2str(I) '_' num2str(derate) ' = DTU10MWRWT;'])
When seed,wind, I, derate take integer values, the function gives what I want, but when these variables take rational noninteger values, it gives me this error:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Any feedback is very much appreciated.
1 Kommentar
Stephen23
am 24 Aug. 2022
Bearbeitet: Stephen23
am 24 Aug. 2022
"Any feedback is very much appreciated."
In general, will converting "rational noninteger values" to text produce valid variable names? (hint: no).
For example, is the text "1.23e-4" part of a valid variable name? (hint: no, the dot and minus are not permitted).
Your data design forces you into writing complex, buggy, slow, fragile, inefficient code.
Solution: do not force meta-data into variable names.
Meta-data is data. Data should be stored in variables, not in variable names.
Akzeptierte Antwort
Mathieu NOE
am 24 Aug. 2022
hello
your method works only for integers
otherwise you have to round your numbers with a given number of decimals befoe it hets converted into char but the dot character is not supported in names so I changed it to a "d" character
see example below for "seed" (other parameters are integers here for sake of simplicity) :
DTU10MWRWT = rand(1);
seed = 3*rand(1);
seed_str = num2str(1+0.01*round(seed*100)); % value rounded with 2 decimals
seed_str = strrep(seed_str,'.','d');
out_name = ['DTU_10MW_RWT_s' seed_str '_' num2str(2) '_' num2str(3) '_' num2str(4)];
eval([out_name ' = DTU10MWRWT;'])
% %% alternative to eval
%
% variableCreator ( out_name, DTU10MWRWT )
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function variableCreator ( newVar, variable )
% assignin ( 'caller', newVar, variable );
% end
%
3 Kommentare
Mathieu NOE
am 24 Aug. 2022
hi @Stephen23
ok - so this something I have to remember , tx !
a simple reaction when I saw the "eval" not-so-appreciated usage.
Stephen23
am 24 Aug. 2022
Bearbeitet: Stephen23
am 24 Aug. 2022
"a simple reaction when I saw the "eval" not-so-appreciated usage."
I wrote here about forcing meta-data into variable names, aka dynamic variable names. Variable names can be created dynamically using various methods and functions (e.g. EVAL, ASSIGNIN, LOAD, etc), and they all suffer exactly the same problems:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!