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

조회 수: 2 (최근 30일)
giovanni bonciani
giovanni bonciani 2020년 2월 11일
댓글: Darshan Ramakant Bhat 2020년 2월 19일
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
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
Hope this will be helpful
  댓글 수: 2
giovanni bonciani
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
Darshan Ramakant Bhat 2020년 2월 13일
Which version of MATLAB are you using ? It worked for me in R2019b

댓글을 달려면 로그인하십시오.


Giovanni Bonciani
Giovanni Bonciani 2020년 2월 13일
If you see down in "release" field, i wrote it, it's 2019b
  댓글 수: 3
giovanni bonciani
giovanni bonciani 2020년 2월 13일
i tried to generate .mex file, but it failed:
>> codegen myFunc -report
??? Unable to copy file from 'C:\Users\admin\Desktop\codegen\mex\myFunc\myFunc_mex.mexw64' to 'C:\Users\admin\Desktop\myFunc_mex.mexw64'.
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Algorithm Acceleration에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by