MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: calling a C executable from within Mathematica

  • To: mathgroup at
  • Subject: [mg60526] Re: calling a C executable from within Mathematica
  • From: albert <awnl at>
  • Date: Mon, 19 Sep 2005 04:45:43 -0400 (EDT)
  • References: <dgh4ms$nhf$>
  • Sender: owner-wri-mathgroup at

Hi Steve,

> Would someone please help me with the function call within Mathematica?
> I would like to use a C program within Mathematica.
> Here is what the program looks like when run under a DOS window. I have
> included some paratheses where I comment.
it's not completly clear to me what exactly your problem is, but as far
as I understand you have a C-Program that does a calculation and
interactively asks for input and you want to use that program within
mathematica via mathlink. You could try to run the binary 'as is' (without
even using mathlink) from within mathematica in various ways but you will
need tricks to supply the 'interactive' input to stdin of your executable.
E.g. you could write a temporary file with the desired input from within
mathematica. Then run the binary with:
Run["simulation5.ext  alpha.txt params.txt output.txt < tempfile"] 
and read and parse output.txt (which I presume contains the results of your
calculation). I think this should work with Windows but I'm not firm about
the correct syntax of redirecting stdin. 

Since all solutions along these lines are rather hacky, I rather would
recommend to change the C-code, which I understand you have access to, as

Make one C-function that will perform the complete calculation. It will need
to accept all arguments (the various parameters and data-arrays)
as a (rather lengthy) list of arguments. Something close to this could well
be there ready to use and might be called right after the interactive part
of the existing C-program. 

This function should return an array with all relevant output (instead of
writing the results to a file). Any formatting of the output (if necessary)
I would try to do within mathematica and just pass the raw resulting data
back to mathematica.

For this function you will need to supply a mathlink template (see the
section "Setting Up External Functions to Be Called from Mathematica" of
the Mathlink-documentation within the Mathematica-Helpbrowser). This
template takes care of getting the data from mathematica to C and back. To
handle the arrays you will need to look at the section "Handling Lists,
Arrays and Other Expressions" of the mathlink documentation. Maybe you will
need to write some C-code here, in case the structure of your data is more
complicated than what is supplied by the mathlink template mechanism. 

Compile the resulting code using the mcc-script that comes with mathematica
and you are ready to use it. You should start with a simple tryout program
if you never have used mathlink before.

I would also recommend to not pass filenames from mathematica to the
C-program but instead import all data into mathematica and transfere it as
arrays of Reals or Integers to the C-Programm via mathlink. That does not
only save you from all the complications of where to look for the files
witin the C-Code but also gives you better control over the data within
mathematica: reading and preparing any kind of data can be done within
mathematica very comfortably an probably is the reason why you want to
access the code from within mathematica, after all. 

For the relatively small data files in your example you could just
copy and paste the data to a mathematica-notebook, for large data-files you
could use the Import-Function, which can read not only plain text files,
but also CSV- or Excel-Files. You could even access databases using the
DatabaseLink that ships with the newer Versions of mathematica, if that's
where your data originially comes from.

You might also find it handy to provide some of your input using the
option-mechanism of Mathematica instead of calling a function with 2 dozens
of arguments. So maybe a mathematica wrapper-function which constructs the
correct call to the C-Function from the various options might be a good

I hope that helps, also it is rather vague and only a rough abstract of the
mathlink documentation: if you ask more specific questions, you might get
more specific answers :-)


  • Prev by Date: Re: probability with simulation
  • Next by Date: Re: probability with simulation
  • Previous by thread: calling a C executable from within Mathematica
  • Next by thread: NDSolve acceleration by forcing use of LAPACK functions