4
$\begingroup$

Problem description

I've written a function in Matlab as follows:

function z = testfuncadd(x,y)
z = x + y;
end

I want to use this function within Mathematica utilizing Matlink. I tried the two methods below but none of them were useful.

  • Using command MEvaluate["testfuncadd(2,3)"] in Mathematica.
  • Using command MATLABCell[] and the command testfuncadd(2,3).

The two methods above have a common error information Undefined function 'testfuncadd' for input arguments of type 'double'.

I've already added the folder that contains the function testfuncadd.m into the Matlab Path. But this still didn't solve the question. On the other hand, I can successfully call the built-in function of Matlab within Mathematica such as eig using the command MEvaluate["eig(magic(3))"].

What else can I do to solve this problem?

Version information

  • Matlab: 23.2.0.2391609 (R2023b) Update 2
  • Mathematica: "13.1.0 for Microsoft Windows (64-bit) (June 16, 2022)"
  • Windows 10
$\endgroup$
2
  • $\begingroup$ I would suspect path problems. What does MEvaluate["path"] give you? $\endgroup$
    – mikado
    Commented Dec 22, 2023 at 13:49
  • $\begingroup$ @mikado Thank you for your comment. I've figured out the solution(I just made it work well while I don't know the exact reason). $\endgroup$
    – narip
    Commented Dec 22, 2023 at 14:28

1 Answer 1

2
$\begingroup$

I finally solved the problem but I'm not pretty sure about the reason. As suggested by @mikado in the comment, this time I used the command

addpath("your folder directory which contains the function you want to implement")

in Matlab instead of using enter image description here button in Matlab. Things just worked out and I don't know why.

Anyway, to run a personal function in Matlab within Mathematica utilizing MATLink, you just need to make sure the package MATLink is installed as documented and add the folder directory that contains the function you want to implement into the path of Matlab. You can try both the command method addpath or the GUI button add Set Path.

Edit

Using addpath command in Matlab might not really add the required file to the path. One only needs to do it in Mathematica, in other words, running the following code in Mathematica

MEvaluate["addpath(\"Your needed file path\")"];

Then, I think everything will work as expected.

$\endgroup$
1
  • 2
    $\begingroup$ could you please give the complete answer with details. It will be useful for others if you type exactly what you have done within Mathematica. $\endgroup$ Commented Dec 23, 2023 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.