Unexpected matlab operator when using Matlab Coder

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 件のコメント

Aditya
Aditya 2024 年 6 月 28 日
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 2024 年 6 月 28 日
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

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

回答 (1 件)

Aditya
Aditya 2024 年 6 月 28 日

0 投票

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 件のコメント

nirwana
nirwana 2024 年 6 月 28 日
Thanks for providing solution, but when I try to generate mex file by matlab coder using your solution, some another error appear
line 8 : The extrinsic function 'perms' is not available for standalone code generation. It must be eliminated for stand-alone code to be generated. It could not be eliminated because its outputs appear to influence the calling function. Fix this error by not using 'perms' or by ensuring that its outputs are unused.
line 16 : The extrinsic function 'coder.internal.mxSubscript' is not available for standalone code generation. It must be eliminated for stand-alone code to be generated. It could not be eliminated because its outputs appear to influence the calling function. Fix this error by not using 'coder.internal.mxSubscript' or by ensuring that its outputs are unused.
the solution that matlab coder offer ("not using perm or not using coder internal") is something that might affect the result.
Other things that I should add is I use input tipe as below
data double(:inf x 1) --> I use infinity because this function will applied to another data that the length may vary.
m double(1 x 1)
L double(1 x 1)
Does the error msg that appear affected by input type that I use?
Aditya
Aditya 2024 年 7 月 1 日
could you tell me the MATLAB version that you are using?
nirwana
nirwana 2024 年 7 月 1 日
I use R2022b

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

質問済み:

2024 年 6 月 28 日

コメント済み:

2024 年 7 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by