Unexpected matlab operator when using Matlab Coder
4 views (last 30 days)
Show older comments
I use Matlab Coder to generate mex file, but in the middle of process I got error message "Unexpected MATLAB operator" that is indicate ":" in my code
if (abs(permlist(jj,:)-iv))==0
c(jj) = c(jj) + 1 ;
%end
end
Does anyone know how to solve it?
2 Comments
Aditya
on 28 Jun 2024
Hi Nirwana,
I have tried to use the same code for code generation, and it was working fine without any issues. Below is the sample code that I have tried:
function c = test_reproduce_error(permlist, iv)
% Initialize output array
c = zeros(size(permlist, 1), 1);
% Loop through rows of permlist
for jj = 1:size(permlist, 1)
% Problematic line of code
if abs(permlist(jj,:) - iv) == 0
c(jj) = c(jj) + 1;
end
end
end
If possible, could you provide the complete code file that is generating this issue?
Answers (1)
Aditya
on 28 Jun 2024
Thanks for providing the file. It seems that you are using mxArray in your code, which has certain restrictions and rules that we need to keep in mind. Specifically, there are some issues in code generation (codegen) when we use mxArray directly in some expressions. It is better to convert the array to a native MATLAB type, such as double, before using it.
Here's the modified file with the provided solution that you can use:
function result = myfunc(data,m,L)
len = length(data);
no_vector = len - (m-1)*L ;
mm = factorial(m);
coder.extrinsic('perms');
% Generate permutation list using feval
temp_permlist = perms(1:m);
% Preallocate permlist as a native MATLAB array
permlist = zeros(mm, m);
% Extract values from temp_permlist into permlist
for i = 1:mm
for j = 1:m
permlist(i, j) = temp_permlist(i, j);
end
end
c=zeros(1,mm);
a=zeros(1,m);
%loop over number of vectors
for i=1:no_vector
%find co-ordinates of each point in m-space
for j=1:m
a(j) = data(i+(j-1)*L);
end
%sort co-ordinates in ascending order
[~,iv] = sort(a,'ascend'); %"iv" is the indice of sorted co-ordinates
%compare pattern with permutation list
for jj=1:mm
if (abs(permlist(jj,:)-iv))==0
c(jj) = c(jj) + 1 ;
end
end
end
c = c/no_vector;
pe = 0;
for k=1:mm
if c(k) ~= 0
pe = pe - c(k)*log(c(k));
end
end
%normalize
result = pe/log(mm);
please refer to this link on how to use mxArray: Working with mxArray
Hope this helps!
3 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!