0
$\begingroup$

I want to write a .NET program calling Mathematica as a computing engine. But here is a problem that argument a1 is not a constant and its value depends on a1=str1+str2. There is a mathematica function(fun1) I need to call and the argument of fun1 is a string. In Mathematica, I program like fun1["@150"]. But now, the problem is coming. Firstly, I think I can call fun1 like this:

Dim ml As IkernelLink = MathLinkFactory.CreateKernelLink()
ml.Evaluate("fun1[a1]")

But I found it is wrong, because in Mathematica the argument of fun1 is essentially a string. But here we also can not use ml.Evaluate("fun1["a1"]"). How can I solve this problem?

$\endgroup$

2 Answers 2

1
$\begingroup$

If your argument is a string, you just need to place a backslash before the quote.

ml.Evaluate("fun1[\"a1\"]")

This also works for declaring variables and using them in a later calculation:

ml.evaluate("a1=\"string1\"<>\"string2\"");
ml.waitForAnswer();
ml.getString();
ml.evaluate("StringTake[a1,13]");
ml.waitForAnswer();
result = ml.getString();
$\endgroup$
3
  • $\begingroup$ thanks ChadK's, but you don't understand the problem. The argument a1 should be defined in the VB program. like I must Dim a1 As String. then I will call fun1 and pass a1 to fun1. $\endgroup$
    – yyteen
    Oct 27, 2014 at 15:02
  • $\begingroup$ Since you know the form that MathLinkFactory functions expect, can you create them in vb? Maybe something like this "fun1[\" <> a1 <> "\"]" ? $\endgroup$
    – ChadK
    Oct 27, 2014 at 17:50
  • $\begingroup$ Yeah, according to your suggestion, I get it work. Just code like this <ml.Evaluate("fun1["""+a1+"""]")>. Thanks!@ChadK $\endgroup$
    – yyteen
    Oct 28, 2014 at 2:28
1
$\begingroup$

I believe all you need is the following. It doesn't connect to the already running kernel, therefore you will have to define your function like so "fun1[a_]:=Print[a];" in some way. In constructs a new kernel.

ml.Evaluate("fun1[\"a1\"]")

I can't tell for certain by your question though.

$\endgroup$
5
  • $\begingroup$ I have tried just like you said. But in vb.net, it use the "" instead of "\. So I tried ml.Evaluate("fun1[""a1""]"). but it's not work! $\endgroup$
    – yyteen
    Oct 27, 2014 at 14:41
  • $\begingroup$ @yteen it doesn't connect to the already running kernel, therefore you will have to define your function like so "fun1[a_]:=Print[a];" in some way. In constructs a new kernel. $\endgroup$
    – William
    Oct 27, 2014 at 14:43
  • $\begingroup$ I don't think so @LiamWilliam. The link is ok, because I use a constant string instead a1 like ml.Evaluate("fun1[""constant string""]") and it is work. $\endgroup$
    – yyteen
    Oct 27, 2014 at 15:14
  • $\begingroup$ @yyteen you need to post all your code otherwise we can't help. You have either most likely attached the function to the FrontEnd or have it installed in a package somewhere. This is somewhat related and demonstrates how the Kernel doesn't hook in with the FrontEnd and doesn't always work. $\endgroup$
    – William
    Oct 27, 2014 at 16:43
  • $\begingroup$ thank you @LiamWilliam ! I have solved the problem with coding <ml.Evaluate("fun1["""+a1+"""]")> $\endgroup$
    – yyteen
    Oct 28, 2014 at 2:40

Your Answer

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

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