Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
giovanni bonciani
giovanni bonciani el 11 de Feb. de 2020
Comentada: Darshan Ramakant Bhat el 19 de Feb. de 2020
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 :)

Respuestas (2)

Darshan Ramakant Bhat
Darshan Ramakant Bhat el 12 de Feb. de 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 comentarios
giovanni bonciani
giovanni bonciani el 12 de Feb. de 2020
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 el 13 de Feb. de 2020
Which version of MATLAB are you using ? It worked for me in R2019b

Iniciar sesión para comentar.


Giovanni Bonciani
Giovanni Bonciani el 13 de Feb. de 2020
If you see down in "release" field, i wrote it, it's 2019b
  3 comentarios
giovanni bonciani
giovanni bonciani el 13 de Feb. de 2020
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 el 19 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Algorithm Acceleration en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by