0

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?

yyteen
  • 145
  • 6

2 Answers2

1

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.

William
  • 7,595
  • 2
  • 22
  • 70
  • 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! – yyteen Oct 27 '14 at 14:41
  • @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. – William Oct 27 '14 at 14:43
  • 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. – yyteen Oct 27 '14 at 15:14
  • @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. – William Oct 27 '14 at 16:43
  • thank you @LiamWilliam ! I have solved the problem with coding <ml.Evaluate("fun1["""+a1+"""]")> – yyteen Oct 28 '14 at 02:40
1

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();
ChadK
  • 306
  • 1
  • 5
  • 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. – yyteen Oct 27 '14 at 15:02
  • Since you know the form that MathLinkFactory functions expect, can you create them in vb? Maybe something like this "fun1[" <> a1 <> ""]" ? – ChadK Oct 27 '14 at 17:50
  • Yeah, according to your suggestion, I get it work. Just code like this <ml.Evaluate("fun1["""+a1+"""]")>. Thanks!@ChadK – yyteen Oct 28 '14 at 02:28