Not sure why "eval(name of a variable)" cannot be assigned a value
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!