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

7 ビュー (過去 30 日間)
giovanni bonciani 2020 年 2 月 11 日
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 :)

サインインしてコメントする。

回答 (2 件)

Darshan Ramakant Bhat 2020 年 2 月 12 日
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

2 件のコメント

giovanni bonciani 2020 年 2 月 12 日
Thank you for your answer Darshan, but Matlab Coder gives me same error at [z{:}]=ndgrid(c{:}); referring to the colon ":" in z and in c:
"Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression."
So i need a code that can be completely different from what i write upon
Darshan Ramakant Bhat 2020 年 2 月 13 日
Which version of MATLAB are you using ? It worked for me in R2019b

サインインしてコメントする。

Giovanni Bonciani 2020 年 2 月 13 日
If you see down in "release" field, i wrote it, it's 2019b

3 件のコメント

Darshan Ramakant Bhat 2020 年 2 月 13 日
I am sorry I missed to look into the "release" field. I tried the "myFunc" code that I had pasted both in R2019b and R2019a, codegen is successful and below is my output from the codegenerated MEX :
>> codegen myFunc.m -report
Code generation successful: View report
>> myFunc_mex()
ans =
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
I am not sure what I am missing here.
giovanni bonciani 2020 年 2 月 13 日
i tried to generate .mex file, but it failed:
>> codegen myFunc -report
Code generation failed: View Error Report
Error using codegen
moreover, Matlab creates a folder "codegen" in which there are various .c file, so it translates to C language, but i use Matlab Coder tool to translate in C++ language (.cpp).
So, do you have any idea in which way can i write my code?
Darshan Ramakant Bhat 2020 年 2 月 19 日
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.

サインインしてコメントする。

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by