Clear Filters
Clear Filters

Unexpected matlab operator when using Matlab Coder

85 views (last 30 days)
nirwana
nirwana on 28 Jun 2024 at 4:48
Commented: nirwana on 1 Jul 2024 at 6:41
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
Aditya on 28 Jun 2024 at 5:52
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?
nirwana
nirwana on 28 Jun 2024 at 8:00
Hi @Aditya, thanks for the answered, but I still did not solve the issues.
Here is my complete code, It it work well it should give the result 0.9390.
function result = myfunc(data,m,L)
len = length(data);
no_vector = len - (m-1)*L ;
mm = factorial(m);
coder.extrinsic('perms');
permlist = perms(1:m);
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);
I use test_code
clear all
tic
data=load('HHZ__20150101T034000Z__20150101T040000Z.DAT');
myfunc(data,5,3)
toc

Sign in to comment.

Answers (1)

Aditya
Aditya on 28 Jun 2024 at 9:35
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
Aditya
Aditya on 1 Jul 2024 at 6:05
could you tell me the MATLAB version that you are using?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!