How to pass large Arrays into a dotnet library via Matlab?

1 次查看(过去 30 天)
I am calling a dot net function which works in C# directly but just doesnt work via matlab. I am thinking matlab internally converts something when I pass large data. Note that in the dotnet code the Array that is passed is internally converted inplace. i.e Arr when passed into the function and when returned are different. Arr will be converted by the function and I think this is causing the issue. It would be helpful if you can guide me to resolve this issue.
In the below code if nsamples is a large number like 1000. Then when I create an array and pass the result is
% Result of the above code when nsamples is large number like 1000
No method 'FrequencyCalibration' with matching signature found for class
'xxx.CalculationContext'.
% result when I use nsamples a very small number
methods(ctx, '-full')
[uint32 scalar RetVal, System.Double[] p] FrequencyCalibration(xxx.CalculationContext this, int32 scalar numt, System.Double[] p)
p =
10 20 30 40 50
ctx.FrequencyCalibration(2,2*p)
ans =
uint32
524288
The above code directly works in C# just fails to work in Matlab. It would be helpful if someone can guide me in resolving this issue.
thank you for your time.
with regards,
Kartik

采纳的回答

Karan Singh
Karan Singh 2023-9-7
Hi Kartik,
From what I understand based on the code and error message you provided, it seems that you are encountering an issue while calling a specific .NET function (FrequencyCalibration) in MATLAB. The code you shared works correctly in C# but fails to work when called from MATLAB.
Here are a few suggestions to resolve the issue:
  1. Array Initialization: Instead of using NET.createArray to initialize the genArr array. You can try using MATLAB's array creation syntax directly.
For example, you can replace the line genArr = NET.createArray('System.Double', nsamples*probetype); with genArr = zeros(1, nsamples*probetype);.
  1. Check the Method Signature: Ensure that the method signature of the FrequencyCalibration function in the .NET code matches the way you are calling it from MATLAB. Check the number and types of arguments being passed, as well as the return type. Make sure that they match the expected signature in the .NET code.
  2. Data Type Conversion: Verify that you are correctly converting the data types when passing data between MATLAB and .NET. Ensure that the data types of the arguments being passed to the FrequencyCalibration function in MATLAB match the expected data types in the .NET code.
Attached below are some documentation links that you may find helpful:
Hope this helps!
  1 个评论
Stefan Bilz
Stefan Bilz 2023-9-7
Hello Karan,
Thanks for the help. But the issue got resolved. The issue was the usage of unsigned int. Hence the signature mismatch. I didnot know matlab had specific type casting. After type casting everything worked just fine. By default all variables in matlab are int. we need to cast it to unsigned int. otherwise the dotnet functions donot work if they are using unsigned variables
Thank you for your time. have a good day!!
with regards,
Kartik

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 .NET Data Types in MATLAB 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by