Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
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... –  Albert Retey Nov 23 '12 at 9:28
    
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. –  fpghost Nov 23 '12 at 13:10
    
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 –  fpghost Nov 23 '12 at 13:11
    
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... –  Albert Retey Nov 23 '12 at 23:53

1 Answer 1

up vote 3 down vote accepted

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"]
share|improve this answer
    
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 –  fpghost Nov 23 '12 at 20:27
    
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... –  Albert Retey Nov 23 '12 at 23:57
    
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.775`50.*10^4, 5.24`50.*10^3" I don't think the "`50" or the "*10^4" will be compat with c? –  fpghost Nov 24 '12 at 6:55
    
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? –  fpghost Nov 24 '12 at 7:31
    
@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... –  Albert Retey Nov 24 '12 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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