MATLAB Answers

??? Dimension 1 is fixed on the left-hand side but varies on the right ([256 x 256] ~= [:? x :?]) when running codegen command

15 views (last 30 days)
I ran into a weird issue when running codegen in matlab. I am aware that codegen requires you to predefine all the variables used. In this case, I predefined Dec as a cell array of cell arrays with each index corresponding to a m x n matrix.
nD = length(D);
kDec = A;
[m,n] = size(A);
Dec = cell(1,L);
for c = 1:L
Dec{c} = cell(nD-1);
for i = 1:nD-1
for j = 1:nD-1
Dec{c}{i,j} = zeros(m,n);
for k = 1:L
Dec{k} = FraDec(kDec,D,k);
kDec = Dec{k}{1,1};
kDec is predefined as a Matrix and therefore by equating kDec to Dec{k}{1,1}, there shouldn't be any issues in the matrix size allocation since both kDec and Dec have been predefined and each entry to Dec has been instantiated to a matrix of the same size. However when running codegen, I get the error ??? Dimension 1 is fixed on the left-hand side but varies on the right ([256 x 256] ~= [:? x :?]) for line:
kDec = Dec{k}{1,1};
After spending a very long time on this error, I have no idea how I can resolve this issue.


Darshan Ramakant Bhat
Darshan Ramakant Bhat on 27 Oct 2020
Can you please attach the full sample code along with the full reproduction steps. This will help us to reproduce the issue at our end.
Kanha Srivastav
Kanha Srivastav on 28 Oct 2020
I have attached my project files in a zip file. I first run the 'test_SplitBregDebP.m' file to generate variables needed in the 'SplitBregDebP.m' function. From there I run
codegen SplitBregDebP.m -args {g,ker,mu,lambda,tol,frame,Level,wLevel,maxit,img}
in the command window.
Doing this, I get the error. If I run
A = zeros(256,256);
D = cell(1,4);
L = 5;
codegen FraDecMultiLevel.m -args {A,D,L}
In the command window, in order to test the function by itself, I run into a different issue in 'FraDec.m' function:
??? Index expression out of bounds. Attempted to access element 1. The valid range is 1-0
This issue is taking a lot of time to debug and the documentation is quite convoluted.

Sign in to comment.

Accepted Answer

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 28 Oct 2020
I have modified the sample code attached and made it work.
Please use "codes_modified" file and run below command :
codegen SplitBregDebP.m -args {g,ker,mu,lambda,tol,frame,Level,wLevel,maxit,img} -report
The "-report" option will generate a codegen report and that will help you to debug the issue. Please refer below doc :
I have made use of coder.varsize() to make the code work. You can read below doc for more information :
There are some limitations when you are using cell arrays for codegeneration :
Hope this will be helpful for you.


Kanha Srivastav
Kanha Srivastav on 29 Oct 2020
Thank you for this update. The changes you made have fixed the issue with variable size error when running codegen. I gave you a heavily modified code that broke the initial functionality of the code and by reverting to the original code I ran into new errors:
??? Unable to determine that every element of 'd{:}' is assigned before this line.
in the uncommented line 68 in the SplitBregDebP.m file.
Darshan Ramakant Bhat
Darshan Ramakant Bhat on 29 Oct 2020
As I mentioned in my answer, you would have run into the cell array problem as mentioned in the doc :
Please try the methods suggested in the doc.
Please take a look at line 31 and 32 of "FraDecMultiLevel.m" file. Compare the code, before and after the modification that I did. You may follow the same approach for your code.
Kanha Srivastav
Kanha Srivastav on 30 Oct 2020
I followed the same approach you had done in "FraDecMultiLevel.m" in my "SplitBregDebP.m" but still run into the same error. I even tried the approach in the given doc regarding the nullcopy approach but still get similar issues.
C1 = coder.nullcopy(C);
b1 = coder.nullcopy(b);
d1 = coder.nullcopy(d);
error = 0.0;
deltab = zeros(m,n);
for nstep=1:maxit
for innerstep=1:1
% solve u.
for ki=1:Level
for ji=1:nD-1
for jj=1:nD-1
u=abs(ifft2(fft2(lambda*FraRecMultiLevel(C1,R,Level)+BTg)./eigenP)); %converted to non-complex %new error Codegen requires that every cell-array element contained in 'C' be assigned a value before being passed into 'FraRecMultiLevel'. Check assignments to element(s):
%u = zeros(m,n);
% solve d
% size(FraDecMultiLevel(u,D,Level))
C1=FraDecMultiLevel(u,D,Level); %changed name of variable %same LHS size != RHS error
for ki=1:Level
for ji=1:nD-1
for jj=1:nD-1
d1{ki}{ji,jj}=wthresh(C1{ki}{ji,jj}+b1{ki}{ji,jj},'s',muLevel{ki}{ji,jj}/lambda); %Unable to determine that every element of 'C{:}' is assigned before this line.
Could this issue be related to the fact that C1, d1 and b1 are changing in different loops?

Sign in to comment.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!