Am I creating dynamic variable names?

After reading this thread, I am still not sure I have created "dynamic variable names", which should be avoided as suggested in that thread.
I have a code snippet as following:
data = [1, 2, 3];
centerX = data(1);
centerY = data(2);
centerZ = data(3);
% And use these variables afterwards
Should I avoid creating those (probably dynamic) variable names, i.e., centerX, centerY, centerZ to use those data and simply/directly use data(1), data(2), data(3) instead?

3 Commenti

Stephen23
Stephen23 il 21 Nov 2021
Modificato: Stephen23 il 21 Nov 2021
"After reading this thread, I am still not sure I have created "dynamic variable names""
No, you have not.
Absolutely nothing in your code dynamically names any variables or accesses any variable names dynamically.
"Should I avoid creating those (probably dynamic) variable names,"
What makes you think that they are "probably" dynamically named, given that they are hardcoded variable names?
  • matrix1, matrix2, matrix3, matrix4, ...
  • test_20kmh, test_50kmh, test_80kmh, ...
  • nameA, nameB, nameC, nameD, ...
hmhuang
hmhuang il 21 Nov 2021
Modificato: hmhuang il 21 Nov 2021
@Stephen But now I understand those example given in that thread were dynamically created using eval, which is different from my hardcoded ones.

Accedi per commentare.

Risposte (1)

What you are doing here is not dynamic variable naming. You're assigning a new variable name from your already assigned data.
This example shows the dynamic variable naming and assigning values to the dynamically named variables (U and V):
for jj=1:5
eval(['V' num2str(jj) '= jj'])
eval(['U' num2str(jj) '= ' 'V' num2str(jj) '*jj'])
end
Which is NOT recommended to employ.
That is equivalent to:
V = 1:5;
U = V.*V;
This one is the recommended one.

11 Commenti

hmhuang
hmhuang il 20 Nov 2021
Modificato: hmhuang il 20 Nov 2021
@Sulaymon Eshkabilov Thanks! One question: Why one would decide to use the expression with eval if they can use the much simpler and efficient syntax (V = 1:5; U = V.*V). The syntax with eval is complex for me in the first place...
It is common for people to load multiple files and to want to have a full variable named the same thing as the file.
Walter pinpointed one of the key reasons why the dynamic variable naming is employed. In addition to that, some users want to control/name variables in a specific (or sequential) order and a dynamic pace.
"There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors."
"It is common for people to load multiple files and to want to have a full variable named the same thing as the file."
@Walter Roberson Can you please give an simple example?
dinfo = dir('*.png');
filenames = fullfile({dinfo.folder}, {dinfo.name});
for K = 1 : length(filenames)
thisfile = filenames{K};
thisimage = imread(thisfile);
[~, basename, ~] = fileparts(thisfile); %just the name with no folder or extension
At this point, the user wants to use the content of basename (so, just the name part of the image) as the name of the variable to write to. For example if the files were 001IMG.png 002IMG.png 003IMG.png then afterwards they would want variables named 001IMG, 002IMG, and 003IMG .
(No, those are not capital-O at the beginning of the file names, those are zeros... but people would want the variables named starting with a number anyhow.)
Another use that shows up from time to time, is if people have a file that contains the names of variables and associated values. For example they might have an .xlsx file that contains
pressure_ammonia, 0.73
mols_ammonia, 548.19
ambient temperature, 280.4
and they would want out of this
pressure_ammonia = 0.73
mols_ammonia = 548.19
ambient temperature = 280.4
You may notice that ambient temperature contains a space in it. That is irrelevant to the people who want to do this: they would want a variable name with a space in the middle of it to be created.
hmhuang
hmhuang il 22 Nov 2021
Modificato: hmhuang il 22 Nov 2021
@Walter Roberson Thanks for your detailed answer! One more question: so it seems that in this use case we can even "have a full variable named the same thing as the file" without using eval, as your example shown, right?
dinfo = dir('*.png');
filenames = fullfile({dinfo.folder}, {dinfo.name});
for K = 1 : length(filenames)
thisfile = filenames{K};
thisimage = imread(thisfile);
[~, basename, ~] = fileparts(thisfile); %just the name with no folder or extension
No, my code shows as far as extracting the basic filename (no folder, no extension) into the variable named basename but does not show creating a variable that is named the same as the content of the variable named basename . So if basename contained IMG001 then the user would want variable named IMG001 created, but doing that would involve either user some form of eval() or else generating some code into a file and executing the code. And of course if basename contained 001IMG because the file name was 001IMG.png then you simply cannot create a variable named 001IMG -- which should tend to suggest that this is not a wise thing to want to do.
@hmhuang, no. He did not create the dynamically named variable. His variable names are hard coded. If the file was called foo.png, there is no variable created called foo. You could do that with eval() but as we've been discussing, that's not recommended so that's why he probably did not show it. However he's saying that some people think they want to do it because they don't realize the problems it can cause.
That's clear now, thanks!

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Tag

Richiesto:

il 20 Nov 2021

Commentato:

il 22 Nov 2021

Community Treasure Hunt

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

Start Hunting!

Translated by