Reputation: 2570
I am trying to assign values to a dynamic variable using eval
in Matlab. I'm using Matlab R2013a. I can easily do it older versions but not in R2013a. Yes, I'm aware of the pitfalls of using eval
, etc. I tried assignin
as well, but that wasn't successful.
Bins = [10 100 20];
Cols = numel(Bins);
for i = 1:Cols
eval(['Var' num2str(i) ' = Cols+i']);
end
Error --> Attempt to add "Var" to a static workspace.
Desired Output
Var1 = 4
Var2 = 5
Var3 = 6
Upvotes: 0
Views: 1325
Reputation: 18484
Is that code inside of a nested function? See this article from The MathWorks.
If you must create variables that way (there are much better options) then you'll need to do it within the main function or use a sub-function. See this article on sharing data between workspaces for details on the different types of functions and how to use them.
Upvotes: 2