MATLAB: Using assignin with array indexing

array indexingassigninMATLAB

Hello everyone,
I am facing a problem using MATLABs assignin() function. First of all I have heard that it is not recommended by many users to use assignin. However, I think in this case it might be necessary.
What I need to do is read variable names as well as the corresponding values from an EXCEL user interface. The variable names are stored in cel array called "v", the values in cell array called "values". At the moment I am using the following code that works without any problems:
v = {'variableName1','variableName2'};
values = {10,20};
for n = 1:numel(v)
assignin('base',char(v(n)),values{n})
end
The problem is that some variables I want to assign values in are no single values but part of an array. For instance I might want to assign values in the variables "variableName1" and also in the second position of "variableName2". I tried to use the following code but it creates an error as "variableName2(2)" is not a valid variable name.
v = {'variableName1','variableName2(2)'};
values = {10,20};
for n = 1:numel(v)
assignin('base',char(v(n)),values{n})
end
Is there a way to fix this error?

Best Answer

prepare the full assignment statement and use evalin().
Related Question