6
$\begingroup$

I have previously been using

logf[x_, y_] := f[x, y] /. v_ :>(PutAppend[Unevaluated[f[x, y] = v;], "out.txt"];);

which produces output like f[1,5]=2.566665345435; on each line of the output file. How could I modify this to produce CSV file, i.e. output lines that look like

1, 5, 2.5666665345435
3, 4, 4.4353453453453
.
.

I tried the obvious guess

   logf[x_, y_] := f[x, y] /. v_ :>(PutAppend[Unevaluated[x, y, v], "out.txt"];);

but this does not work, as the word "Sequence" also gets exported.

Also is this the best way I can export data from Mathematica to be read in by c++ code or is there a better way?

$\endgroup$
4
  • 1
    $\begingroup$ I think you should explain the big picture of what you are doing, otherwise it is impossible to suggest a "better" or even "good" way. Things of interest are e.g. necessity to write line by line vs. writing on block, efficiency requirements and/or amount of data to read/write and what possibilities (libraries) there are available to read data on the C++ side, will the data be read on the same computer as it is written, do you need the file be human readable also. Every other bit of additional information will help others to give better answers... $\endgroup$ Nov 23, 2012 at 9:28
  • $\begingroup$ Basically this is a function of two variables that sets some initial conditions for each point in the parameter space, i.e. maybe f[1,2]=2.3+9.8I. The c++ code needs to read this in and then I basically want to store in some object that resembles a function (I was thinking of using map with x,y as a key and the complex value of the function at (x,y) as the value but maybe there is a better way). There is no necessity for the file to be human readable, but it could be read on other computers. $\endgroup$
    – fpghost
    Nov 23, 2012 at 13:10
  • $\begingroup$ There is a lot of data, but I don't think space is a massive concern, but if you know a better way to store the data file, and retrieve it in the c++ code so that I have something like a function in c++ I'd be happy to consider going that way $\endgroup$
    – fpghost
    Nov 23, 2012 at 13:11
  • $\begingroup$ as you are storing very uniform data and have a lot of data, you should think about storing those values in a binary file. You'd need a combination of OpenWrite or OpenAppend, BinaryWrite and Close. With BinaryWrite you can store numeric data as reals of various bitlengths or even as complexes. For details you might want to search those functions and BinaryRead in the documentation. It should be relatively easy and very efficient to read these files from c++. As long as the "endianness" is the same it shouldn't be a problem to read those files on another computer... $\endgroup$ Nov 23, 2012 at 23:53

1 Answer 1

7
$\begingroup$

You could pre-generate the data you want to export, for instance:

dataset = Flatten[Table[{x, y, f[x, y]}, {x, 0, 1, .1}, {y, 0, 1.1}], 1];

and then simply use Export:

Export["mydataset.txt", dataset, "CSV"]
$\endgroup$
7
  • $\begingroup$ Is there a way to make sure the numeric values of f[x,y] are in C-style scientific notation,e.g. not 1.7736782427799868006160419897172935521449567176544099319450645855950.*^7` but something like 1.77367824277998680061604198971729355214495671765440993194506458559e^7 i.e. using e and not using the "`50" precision notation $\endgroup$
    – fpghost
    Nov 23, 2012 at 20:27
  • $\begingroup$ Unlike with Put I think what you want is the default when exporting as "CSV". Do you have a counterexample? See also my suggestion to store those numbers as binary which will do away with all those formatting issues... $\endgroup$ Nov 23, 2012 at 23:57
  • $\begingroup$ I've actually decided for various reasons that I'm going to use a shell script to import the ICs then compile and execute the code now, so I do need the data in the form "1, 61/10, 1.775e-4, 5.24e+3,...." after all (sorry). When I do the exporting as VLC suggests above, then what I get out is actually "1,61/10, 1.77550.*10^4, 5.2450.*10^3" I don't think the "`50" or the "*10^4" will be compat with c? $\endgroup$
    – fpghost
    Nov 24, 2012 at 6:55
  • $\begingroup$ Actually Albert I think you are correct regarding the "CSV"; I tried Export["file.csv, data, "CSV"] and it looks good. I think the key is calling file .csv not .txt in addittion to "CSV" option? $\endgroup$
    – fpghost
    Nov 24, 2012 at 7:31
  • $\begingroup$ @fpghost: strange, I would have thought that the explicit format specifier (3rd arg) should entirely overwrite the file extention, but the documentation is somewhat vague about such details. Anyway, it seems to work for me as I expect (that is it is independent of what file extension I use). What OS and version are you using? -- And no reason to be sorry, what you use might well be the best solution for your problem (and of course you are free to use something suboptimal :-). That's why I asked for more details and I just thought you should know the alternatives... $\endgroup$ Nov 24, 2012 at 9:37

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.