MATLAB Answers

# Problem using :(colon operator) and ndgrid in Matlab Coder

4 views (last 30 days)
Hello everyone, i have a problem using a cell array with argument {:} and ndgrid function into Matlab Coder. In detail, in the original code i have:
a matrix:
a=[ 5 185 ; 50 230; 95 275; 140 320];
a cell c made in this way:
c={};
for i=1:length(a)
c{1,i}=[a(i,:)];
end
c =
1×4 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double}
celldisp(c)
c{1} =
5 185
c{2} =
50 230
c{3} =
95 275
c{4} =
140 320
a matrix B with all combinations of c, made in this way:
[c{:}]=ndgrid(c{:});
n=length(c);
B = reshape(cat(n+1,c{:}),[],n);
B = sort(B,2);
B =
5 50 95 140
50 95 140 185
5 95 140 230
95 140 185 230
5 50 140 275
50 140 185 275
5 140 230 275
140 185 230 275
5 50 95 320
50 95 185 320
5 95 230 320
95 185 230 320
5 50 275 320
50 185 275 320
5 230 275 320
185 230 275 320
BUT, using Matlab Coder to translate that code, it gives me this (see down) error referred to the colon ":" inside [c{:}]=ndgrid(c{:}); and inside B = reshape(cat(n+1,c{:}),[],n);
"Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression."
So, i want to obtain B matrix but not using ndgrid (i think about a for-loop istead of it) or any kind of alternative code to do this, specially not using colon ":" except in a for-loop.
Thank you very much :)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Darshan Ramakant Bhat on 12 Feb 2020
I modified the code a little bit and the code generation was successful
function B = myFunc()
a=[ 5 185 ; 50 230; 95 275; 140 320];
c=cell(1,length(a));
for i=1:length(a)
c{1,i}=[a(i,:)];
end
z = cell(1,length(c));
[z{:}]=ndgrid(c{:});
n=length(z);
B = reshape(cat(n+1,z{:}),[],n);
B = sort(B,2);
end
>> codegen myFunc -report
Hope this will be helpful
##### 2 CommentsShowHide 1 older comment
Darshan Ramakant Bhat on 13 Feb 2020
Which version of MATLAB are you using ? It worked for me in R2019b

Sign in to comment.

Giovanni Bonciani on 13 Feb 2020
If you see down in "release" field, i wrote it, it's 2019b
##### 3 CommentsShowHide 2 older comments
Darshan Ramakant Bhat on 19 Feb 2020
From the log it looks like that the MEX creation is successful but it had trouble in copying the generated MEX (may be some permission issue in the folder).
You can change the target language for codegeneration using the below command :
>> cfg = coder.config('lib');
>> cfg.TargetLang = "C++";
>> codegen -config cfg myFunc -d 'myFolder' -report
Please read the below documentation pages to get more insight into the commands :
Hope this will be helpful for you.

Sign in to comment.

R2019b

### Community Treasure Hunt

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

Start Hunting!