I'm trying to compile a mex-file from my m-code. I've problems in the following lines
Atm = sys.A0; Btm = sys.B0; Ctm = sys.C0; Dtm = sys.D0; for i = 1:np Atm = Atm+sys.(['A',sprintf('%.0f',i)]) * p(i,t); Btm = Btm+sys.(['B',sprintf('%.0f',i)]) * p(i,t); Ctm = Ctm+sys.(['C',sprintf('%.0f',i)]) * p(i,t); Dtm = Dtm+sys.(['D',sprintf('%.0f',i)]) * p(i,t); end
which returns the error '??? Non-constant expression or empty matrix'.
The variable sys is a structure with field names sys.A0, sys.A1,…, sys.A[np], sys.B0, …, sys.D[np], sys.np. Where np may vary from case to case and the variables sys.A[i],…,sys.D[i] are 2-D matrices. The variable p is a matrix of size np X m. I compile the code for a fixed sized variable sys and p, with
codegen errAndJac -args {u,p,y,sys}
My problem seems the following. The code generation cannot handle ['A',sprintf('%.0f',i)] to locate a certain matrix. I've also tried to first pre locate a variable iA = char('A0',…,'A[np]') and then use sys.(iA(i,:)), which resulted in the same error.
An alternative and/or solution will be highly appreciated. Thanks in advanced. Attached is the function to compile and an example of my variables.
Best Answer