Expected either a logical, char, int, fi, single, or double. Found an mxArray.
9 ビュー (過去 30 日間)
古いコメントを表示
function C = fcn(f,U_b)
k = 2*pi*f/U_b;
%Theodorsen fcn C
%besselh is the second kind Hankel fcn
%Declare besselh as extrinsic
coder.extrinsic('besselh');
C = complex(0);
C = besselh(1,2,k)/(besselh(1,2,k)+1i*besselh(0,2,k));
I am using the Matlab fonction block above. I'm using an extrinsic function because 'besselh()' was not supported for code generation. I have defined C as a complex since it's the value that I am expecting it to return (https://fr.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47), but I'm still getting this error:
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
Thank you.
0 件のコメント
回答 (1 件)
Rajanya
2024 年 12 月 19 日
I was able to reproduce the error. The reason for this error is that when you declare a function as ‘extrinsic’, the function is dispatched to MATLAB environment for execution, producing an ‘mxArray’ as the result, and the only supported operations for an ‘mxArray’ are storing it in a variable, passing it to another function or returning it to MATLAB - https://www.mathworks.com/help/simulink/slref/coder.extrinsic.html#:~:text=Define%20the%20local%20function%20returnStringToNumber,mxArrays%20(MATLAB%20Coder).
Here, ‘besselh’ is declared as an ‘extrinsic’ function, so its output is an ‘mxArray’. Hence, the use of ‘besselh’ directly in an arithmetic expression in the following line throws the error.
C = besselh(1,2,k)/(besselh(1,2,k)+1i*besselh(0,2,k));
Assigning the ‘besselh’ results to variables and converting them to a known type before evaluating the expression resolves the error since mxArrays become valid for other operations only when they are converted to a known type before use - https://www.mathworks.com/help/coder/ug/use-matlab-engine-to-execute-a-function-call-in-generated-code.html#bq1h2z9-46:~:text=To%20use%20an%20mxArray%20returned%20by%20an%20extrinsic%20function%20in%20other%20operations%20(for%20example%2C%20returning%20it%20from%20a%20MATLAB%20Function%20block%20to%20Simulink%20execution)%2C%20you%20must%20first%20convert%20it%20to%20a%20known%20type
Thanks! Hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!