Not sure why "eval(name of a variable)" cannot be assigned a value

38 views (last 30 days)
Hello,
I'm trying to reorganize a series of vectors according to a precalculated order stored in a hash table. The names of the variables are stored in a cell array. I think that what I'm trying to do is an array of pointers. I want to process all the variables iteratively, but I cannot figure out how.
Here's my procedure:
  • copy the variable to process into a temporary variable, using its name stored in the cell (eval function),
  • create an ordered variable,
  • process the temporary variable and store the ordered values into the ordered variable,
  • copy the ordered variable into the original one, using its name, again.
And here's what it looks like:
variablesToReorder = {'Branchlength', 'Euclideandistance', 'V1x', 'V1y', 'V2x', 'V2y', 'VXc', 'VYc'};
for i = 1 : numel(variablesToReorder)
name = (variablesToReorder{i});
tempVar = eval(name);
orderedVar = tempVar;
for j = 1 : length(tempVar)
orderedVar(hash(j,1)) = tempVar(hash(j,2)); % Order the current variable according to the hash table
end
eval(name) = orderedVar; % This is where it crashes
end
I get a "Subscripted assignment dimension mismatch" error. Besides, Matlab considers the second "eval" as a variable that changes size at each iteration. It looks like writing A = eval(B) is okay, but not the invert.
I know that using eval isn't a good idea, but I cannot figure how to do otherwise. Structure array?
So I would be very grateful having ideas on alternate solutions.
Thank you for your attention.

Accepted Answer

Stephen23
Stephen23 on 14 Jun 2016
Edited: Stephen23 on 14 Jun 2016
This is why beginners love eval: because they think it solves problems. In reality it just makes problems that are hard to debug because using eval deactivates all of the useful debugging tools that MATLAB has. Once you learn to write faster, neater code without using eval, and start paying attention to those wavy orange underlines, tab completion, f1 help, mlint messages, etc, etc, you will wonder why beginners bother to use eval, when it removes all of the tools that help them to write code, and find and fix mistakes.
If you must use names then a much better solution would be to keep all of those variables as fields of a structure. Then looping over them would be neater and faster:
S.name1 = [...];
S.name2 = [...];
C = fieldnames(S);
for k = 1:numel(C)
S.(C{K}) = sort(S.(C{K}));
end
Or indeed any kind of array would be better than using eval, with simple indexing: a non-scalar structure, a cell array, or a numeric array.
  2 Comments
Nicolas
Nicolas on 14 Jun 2016
Thank you for your answer, this is the solution I was looking for.
My question was less about debugging, but rather why A = eval(string) works, while eval(string) = A doesn't.
Anyway, I won't use eval ever again.
Stephen23
Stephen23 on 15 Jun 2016
@Nicolas: eval cannot be used only on the LHS side of an allocation. It can be used to evaluate either:
  • the complete allocation (LHS = RHS), or
  • only the RHS of the allocation.
This is shown in the eval help page.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!