Is it possible to assign variables in the workspace to other variables using for loop?
Mostrar comentarios más antiguos
I have variables like a_1_1, a_1_2, a_1_3, a_2_1, a_2_2, etc...... in the workspace. I want to assign these variables to another variable inside a for loop. Such that for each loop each variable will be loaded. For instance,
for i=1:100
for j=1:3
e=a_i_j
end
end
The abovementioned is just a pseudo code only for understanding.
How this can be done inside a for loop?
1 comentario
"I have variables like a_1_1, a_1_2, a_1_3, a_2_1, a_2_2, etc...... in the workspace"
Why?
Forcing meta-data into variable names makes accessing your data slow, inefficient, buggy, and complex:
So far you have not told us the most important information: how did you get all of those variables into the workspace? The place where those variables are loaded or imported into the workspace is the correct place to fix this bad data design. For example, by LOADing into an output variable:
S = load(..);
Respuesta aceptada
Más respuestas (1)
Ayush Modi
el 21 de Mayo de 2024
Editada: Ayush Modi
el 21 de Mayo de 2024
Hi Bipin,
Yes, it is possible. To dynamically access variables such as a_1_1, a_1_2, a_1_3, a_2_1, a_2_2, etc from the workspace within a loop, you can use the eval function.
Here is an example to demonstrate how you can achieve this:
for i = 1:100
for j = 1:3
varName = sprintf('a_%d_%d', i, j);
e = eval(varName);
...
end
end
Refer to the following MathWorks documentation for more information on "eval" function:
7 comentarios
BIPIN SAMUEL
el 22 de Mayo de 2024
Stephen23
el 22 de Mayo de 2024
"is there any alternative way to do this instead of using 'eval'?"
Yes: by not forcing meta-data into variable names. We could show you how, if you answered my question here:
Fake Name Johnson
el 22 de Mayo de 2024
Sure, anything is possible, but even the docs will tell you that it's a bad idea. I don't know why this is a recommendation.
The link given leads us here:
Variables with Sequential Names
A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array. If the data sets are of different types or sizes, use a structure or cell array.
...
When somebody says "I'm painting the floor but the walls keep getting closer", the answer isn't "don't worry, it's possible to keep painting until you reach the wall". The answer is "stop painting yourself into the corner".
BIPIN SAMUEL
el 23 de Mayo de 2024
Stephen23
el 23 de Mayo de 2024
"These variables are from the database which was already created before... "
You still have not answered the question: how did that data get into the workspace?
"... and dynamic naming was required for that data to give the labeling accordingly."
It is very unlikely that forcing meta-data into variable name was required. Much better data design would simply and efficiently use actual indices, rather than awkwardly and inefficiently forcing pseudo-indices into variable names.
BIPIN SAMUEL
el 23 de Mayo de 2024
"data was loaded using load function."
Do not LOAD directly into the workspace. Always LOAD into an output variable:
S = load(..);
S will be a scalar structure. You can access its fields using e.g.:
C = fieldnames(S);
S.(C{1}) % the first field
or generate the fieldnames using e.g. SPRINTF.
Much better: use actual indices rather than forcing pseudo-indices into variable names.
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!