Hi,
I was wondering if anyone could suggest a quicker way of solving this problem.
I have a function that generates an 'n x n' cell array (A) where each cell contains a string. These strings are equations for calculating variables. For example, if n=3, A =
'theta(1).*theta(1)' 'theta(1).*theta(2)' 'theta(1).*theta(3)''theta(2).*theta(1)' 'theta(2).*theta(2)' 'theta(2).*theta(3)''theta(3).*theta(1)' 'theta(3).*theta(2)' 'theta(3).*theta(3)'
where theta is a predefined variable.
I have tried the 'eval' function in a loop for this and it works.
for i = 1:n for j = 1:n a(i,j)=eval(A{i,j}); endend
However, this code is used in an optimization and I have found the optimizer is much quicker if I manually copy and paste each string and type within the .m file:
a11 = theta(1).*theta(1)a12 = theta(1).*theta(2)a13 = ...
The problem with this approach is that I would like to automate the code so that 'n' can be increased to any value so doing it manually would not be pratical for high values.
Is there a way to get matlab to write these strings into an m file once they've been generated? Sort of 'copying and pasting' but getting matlab to do the hard work! I've been looking but can't seem to find one.
Any feedback anyone could give would be greatly appreciated.
Thanks,
Mike
Best Answer