MATLAB: Getting an error: this statement is incomplete

erroreval

Ive been getting this error for the 'eval' line and i'm not sure in what way the statement is incomplete.
% Giving initial values vector (element for each layer) of each state variable.
for i=1:length(R(idR).St.StNames),
eval(strcat('R.St.', char(R(idR).St.StNames(i,:)),' = ', char(R(idR).StM(i,:))));
end

Best Answer

Important rule: Never use eval
And certainly not for generating dynamic fields when there is a more readable, lots easier to debug, alternative
Also a lot easier to debug: sprintf or, in recent versions, compose instead of concatenating bits of strings together.
Another issue is that it would appear that StNames is 2D (since you have written StNames(i, :)), yet you use length on it which will either return the number of rows or columns, whichever is greater. It's also not clear why it is converted to char.
One more potential problem, you're indexing R everywhere but in strcat('R.St.'. This is clearly not going to work. This is why we say not to use eval. You can't debug that line, and you get not help from the editor.
Not knowing what's in StNames, a guess of a fix:
for row = 1:size(R(idR).St.StNames, 1)
R(idR).St.(char(R(idR).St.StNames(row, :)) = char(R.(idR).StM(row, :);
end
Those char conversion are very suspicious though. Even if the above works, there's probably a much clearer way to write it.
I would also recommend that you use longer names for your fields, ones that have meaning. Finally, I would avoid mixing case as you have. It's a pain to write.