# Tag Info

## Hot answers tagged external-calls

46

2014-04-12 NOTICE: MATLAB R2014a contains a bug that breaks MATLink on OS X and Linux (Windows is fine). If you use MATLink on OS X or Linux, please consider keeping MATLAB R2013b until R2014b comes out. Due to the nature of the problem there is no quick workaround that we could apply in MATLink. For full compatibility with Mathematica 10, please upgrade ...

21

On Windows 7 using ReadList instead of Run suppresses the window: Table[Pause[1/2]; ReadList["!dir", String], {3}]; This use of "!command" in place of a file is at least partially documented under OpenRead: On systems that support pipes,OpenRead["!command"] runs the external program specified by command, and opens a pipe to get input from it. As ...

17

Get all the files here: http://JeremyThompson.net/Rocks/Mathematica/MmaWord.zip .Net Mathematica Word Library You will need to use a Microsoft library to open word documents. In a language such as .Net it is very easy; just open Visual Studio, reference the Microsoft.Office.Interop.Word .Net DLL (for Words) and the C:\Program Files\Open XML ...

15

You can call an external (shell) command cmd without showing a command window by using the pipe syntax "!"<>cmd. This can be used in place of a filename with any Mathematica function that opens a file for reading. For example: Import["!dir", "Text"] Read["!dir"] (opens stream, must be closed) OpenRead["!dir"], followed by Read, ReadList etc to grab ...

11

You will find a complete, step by step description of how to write such a program here. Here's a small test program that adds 42 and 137, written by following the tutorial closely: /* mlcall.c */ #include <mathlink.h> #include <stdio.h> int main(int argc, char *argv[]) { MLENV env; MLINK link; int errno; int packet; env = ...

10

Here I show the basic way to call MATLAB using NETLink under Windows via the MATLAB COM interface. This answer is Community Wiki, feel free to extend it to others platforms and/or improve it! In[1]:= Needs["NETLink"] matlab = CreateCOMObject["matlab.application"] Out[2]= «NETObject[COMInterface[MLApp.DIMLApp]]» Now one can invoke MATLAB functions: ...

8

The problem has nothing to do with OpenWrite. You never Close the stream you open in your call to Read. Read, unlike ReadList, does not automatically close a stream (file, pipe, etc.) that's given as its first argument string. (That's because the purpose of Read is to be able to read from the same source in pieces, unlike ReadList which does it all at ...

8

The number 2045 is suspicious. Add three to it to include stdin, stdout, and stderr, and you get 2048, which I suspect is total number of file descriptors available to you. I conclude your problem is caused by eating up all the available file descriptors. This is usually caused by doing too many file opens without doing any file closings to return some file ...

8

I would say JLink is one of the fastest ways to do this. Just use the Runtime to start a process executing your command and collect the exit code too: << JLink RunThroughWithExitCode[cmd_String] := JavaBlock[Module[{ireader, istream, runtime, process, reader}, LoadJavaClass["java.lang.Runtime"]; runtime = RuntimegetRuntime[]; process = ...

8

Under most operating systems you can use the 2>&1 handle redirection operator(1),(2): Import["!foo 2>&1", "Text"] "'foo' is not recognized as an internal or external command, operable program or batch file."

7

I'll leave this up on GitHub, but I won't maintain the port. I recommend using MATLink instead. There's a package on the Wolfram Library Archive called mEngine that allows calling MATLAB from Mathematica. What it can do is: execute arbitrary MATLAB commands and retrieve their output as a string transfer array variables between Mathematica and MATLAB ...

7

The command Run will do exactly that. For example, try Run["touch ~/Desktop/blankfile"] If you want to read the results back in, there are a few options, and the choice between them depends on exactly what you want to do. The RunThrough command lets you read the output of a command-line back as a Mathematica expression. For example, try ...

7

Since the CellEvaluationFunction is supposed to accept two arguments, the second being the format type, I recommend defining: GFortran[inp_, _] := . . . and then using it directly: CellEvaluationFunction -> GFortran

6

Using a slightly modified version of vngx-jsch (source included), an open-source implementation of jsch, and JLink and a small but efficient Mathematica package this is now easily possible. All code can be browsed here, and most simply be installed by executing these lines: (tested on Windows and Linux, not on Mac). It should all work on Mathematica 7, 8 or ...

6

Table[Pause[1/2]; Import["!dir","Text"];, {3}]; Import can also be used to pipe command line output straight into Mathematica. <<"!dir" Also works.

5

The problem is that the stdin stream for each process is being left open and needs to be closed. To demonstrate the issue, we create a single process: $process = StartProcess[$SystemShell]; WriteLine[$process, "echo example line"]; KillProcess[$process]; The call to WriteLine implicitly creates a stream object. Even though the process has been killed, ...

4

We had a bug like this on v. 10: it specifically stopped running new processes after you created 100 of them. This was fixed in version 10.0.1. I recommend testing it there, as version 10.0.1 has a number of improvements with respect to StartProcess. I can't test your example in MacOS right now, so please let me know if this isn't working on v. 10.0.1. ...

3

I solved the problem! The solution is quite simple. It is not allowed to distribute the definition of an indirect function to the sub-kernels. Thus, if we do not use ceteris paribus In[17]:= (* DistributeDefinitions[callVert] *) then setting the option DistributedContexts :> None by In[18]:= Map[SetOptions[#,DistributedContexts :> None]&, ...

3

As Mr.Wizard pointed out, you can do this with ParallelSubmit. But you need a little more, and that's the (not so) tricky part as it is not very well documented. I think something like the following should work for you: Needs["ParallelDeveloper"] f[x_] := (Pause[x]; x) LaunchKernels[1] DistributeDefinitions[f] eid = ParallelSubmit[f@5] QueueRun[] Now ...

2

I faced the same problem but I was told here that it was impossible. So, looking for other alternatives I've found it! It's called CoolProp. Given the source files and a Mathematica link file, you need to build a Mathematica-compatible CoolProp.dll yourself. Get them from GitHub Now, I can use it with ease.

2

I googled ".net change working directory", and this link to Microsoft's .NET docs was the first hit: http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory(v=vs.110).aspx. It would suggest the following: LoadNETType["System.IO.Directory"] DirectorySetCurrentDirectory["c:\\foo"] To find out the current working directory: ...

2

I would do the following. file = OpenWrite[FileNameJoin[{$TemporaryDirectory, "testfile.bat"}]] WriteString[file, "call path"] And then use SystemOpen to call the file. 2 Parallel Kernels for separate Notebooks Perhaps you simply want to run two kernels in parallel. You can do this by: Open Evaluation > Kernel Configuration Options... and set up more than one kernel. Assign a different kernel to each of two Notebooks using Evaluation > Notebook's Kernel From there you can run your slow code in one Notebook and do your ... 1 As Anon pointed out, my problem was more of a poor knowledge of Unix rather than incomplete Mathematica chops, and a judicious use of pipes solves the issue. Thus, instead of exporting a string to a file and using that as an input, as I had, inputstring="sample input data"; Export["inputFile.txt",inputstring,"Text"] inputstream=OpenRead["! ... 1 On Linux you can add the directory containing this library to LD_LIBRARY_PATH. A semi-automated way to do this from a command prompt is this: MATHLINK=$(dirname $(readlink -f$(which math)))/../SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATHLINK You can use this merthod of detecting ...

Only top voted, non community-wiki answers of a minimum length are eligible