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 would like to export a phrase to my result data as a heading.

I'm exporting the data correctly with

 Export["file.dat", table]. 

But I'm doing several simulations with different parameters and I would like to write down this parameters in the file. I'm trying with

 Write["file.dat", {"Resonance simulation Data =", DateString[], "Parametros: Nz = ", Nz, " CP = ", CP, " K1 = ", K1}]; 

but this erases the precedent data if it's put after the Export or it is erased if it came before. How could I do this?

share|improve this question
    
Take a look at OpenAppend[file] –  belisarius Jan 25 '13 at 4:29
    
Maybe Import the file, Insert the new line, and then Export the file again? For example, Export["file.dat", Insert[Import["file.dat"], firstline, 1]] where firstline= {"Resonance simulation ...}. –  kguler Jan 25 '13 at 4:31

3 Answers 3

You could use OpenAppend[] and Write[] instead of Export[] to persist your data.

fn = "c:\\test.dat";
s = OpenWrite@fn;
Write[s, "**Header 1 **"];
Close[s];
s = OpenAppend@fn;
Write[s, Table[i, {i, 10}]];
Close[s];
s = OpenAppend@fn;
Write[s, "**Trailer 1 **"];
Close[s];
FilePrint[fn]

"*Header 1 *"
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
"*Trailer 1 *"

share|improve this answer

You can open a stream, write your header, and then pass the stream to Export:

width = 7;
height = 4;
max = 6;
table = RandomInteger[{1, max}, {width}];
st = OpenWrite["file.dat"] ; Write["file.dat", {DateString[], 
  "width = ", width, " height = ", height, " max = ", max}];
Export[st, table];
FilePrint["file.dat"]
share|improve this answer
1  
Is that syntax (using a stream in Export[] and not a filename) documented somewhere? –  belisarius Jan 25 '13 at 6:59
1  
I don't think it is documented, so this is a "use with caution" answer, as it may change in future versions. Although I would like to see it documented and officially supported, because it makes Export that much more flexible, as here. –  Joel Klein Jan 29 '13 at 19:24
    
Fully agree. Thanks! –  belisarius Jan 29 '13 at 21:22
    
Great to know! Thanks –  sebhofer Aug 2 '13 at 10:21

You can use the following steps to prepend your headers:

  1. import the file using Import
  2. prepend your header using Prepend
  3. Export the data using Export

    data = Import["file.dat"]

{{1, 2}, {3, 4}, {5, 6}}

headers = {"param1","param2"};
data = Prepend[data,headers]

{{"param1", "param2"}, {1, 2}, {3, 4}, {5, 6}}

Export["file.dat",data];
share|improve this answer

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.