C# with matlab via COM

3 ビュー (過去 30 日間)
xiaoli
xiaoli 2024 年 5 月 29 日
コメント済み: xiaoli 2024 年 6 月 1 日
不知道为什么,I run official's example,却提示我以下信息。若是使用Execute直接执行.m代码是可以的,不知道为什么执行.m写的的function就不可以,在C#中我也引用了MLAPP
未定义与'double’类型的输入参数相对应的函数myfunc
but I indeed write myfunc function in E:\modelfirst
.m code:
function [x,y] = myfunc(a,b,c)
x = a + b;
y = sprintf('Hello %s',c);
C# code :
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(@"cd E:\modelfirst\myfunc");
object result = null;
matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world");
object[] res = result as object[];
Console.WriteLine(res[0]);
Console.WriteLine(res[1]);
Console.ReadLine();
find help
thank you!!!

採用された回答

SACHIN KHANDELWAL
SACHIN KHANDELWAL 2024 年 5 月 30 日
One possible issue could be related to the directory path. In your C# code, you are attempting to change the directory to 'E:\modelfirst\myfunc' using 'matlab.Execute(@"cd E:\modelfirst\myfunc");'.
This appears to be incorrect because "myfunc" is the name of the function you're trying to call, not a directory. You should change the directory to 'E:\modelfirst', where your '.m' file is located.
The corrected line should be :
>> matlab.Execute(@"cd E:\modelfirst");
If you are using MATLAB R2022b or a later version, I recommend utilizing the MATLAB Engine API for .NET. This API provides an interface between .NET programming languages and MATLAB.
To set up your .NET environment for building engine applications, please refer to the following MathWorks documentation:
I hope you can now proceed with your workflow.
  1 件のコメント
xiaoli
xiaoli 2024 年 6 月 1 日
TK so much,useful!
I get it

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange.NET Methods in MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by