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.

the problem related to this question

What I want to do is to Export definition of several symbol into a single file. The reason I choose Export is that Export supports the GZIP function.

So here is the example: I have

ff[1, 1] = 1;
ff[1, 2] = 2;
gg[1, 1] = 3;
gg[1, 2] = 4;

if I use Save, then it is simply

Save["ffgg.m", {ff, gg}]

the content of ffgg.m file

ff[1, 1] = 1
ff[1, 2] = 2
gg[1, 1] = 3
gg[1, 2] = 4

But if I want to Export, since the syntax of Export is Export["file", expr]. It seems that I can only Export one expr each time. I tried the following code:

Export["ffgg2.m", {FullDefinition @ ff, FullDefinition @ gg}];

the content of ffgg2.m will be

(* Created by Wolfram Mathematica 8.0 : www.wolfram.com *)
{ff[1, 1] = 1
ff[1, 2] = 2, gg[1, 1] = 3
gg[1, 2] = 4}

And if I Import ffgg2.m back, I only get ff[1,1] and gg[1,1]. The other two lost.

So how can I Export several definitions into a single file at a single time?

share|improve this question
    
matheorem, my code in the PutAppend section was broken on Symbols with OwnValues assignments; my attempt to keep these unevaluated was poorly conceived. I believe I have corrected the problem. –  Mr.Wizard Jul 29 '13 at 8:38
add comment

1 Answer

up vote 9 down vote accepted

FullDefinition

You can use the undocumented(?) multi-argument syntax of FullDefinition:

ExportString[FullDefinition[ff, gg], "Package"]
(* Created by Wolfram Mathematica 7.0 : www.wolfram.com *)

ff[1, 1] = 1

ff[1, 2] = 2

gg[1, 1] = 3

gg[1, 2] = 4

You will notice that any arguments of FullDefinition after the first will be colored, by default in red. This usually signifies a syntax error, but more specifically here it means the syntax doesn't match the pattern given in SyntaxInformation. There are a number of functions that work with syntax somewhat different form that which is officially supported (documented). Of course such functionality may change or be removed in later versions so you have to weigh the alternatives. If you are comfortable with this caveat you can eliminate the red highlighting with:

Unprotect[FullDefinition]

SyntaxInformation[FullDefinition] = {"ArgumentsPattern" -> {__}}

Protect[FullDefinition]

The OP asked:

I searched some questions relate Undocumented functions. Many people suggest "don't use it". So is there any other way to realize the same thing?

Yes, but none that I know of that is as concise.

PutAppend

You can use PutAppend to append definitions to a file:

Cases[Hold[ff, gg], x_ :> (FullDefinition@x >>> "file.m")];
(* contents of file *)
ff[1, 1] = 1

ff[1, 2] = 2
gg[1, 1] = 3

gg[1, 2] = 4

This uses multiple "export" (or rather PutAppend) commands, but it seems to do what you want.

Even more manually you can ExportString each definition then concatenate them:

strings = Cases[Hold[ff, gg], x_ :> ExportString[FullDefinition@x, "Package"]];

Export["file.m", StringJoin @ strings, "Text"]  (* note "Text" format *)
share|improve this answer
    
WoW, "FullDefinition[ff,gg]"! I tried this before, But I found that the second argument of FullDefinition is Red, So I give up. But I encourage to try it a second time. Export["ffgg3.m",FullDefinition[ff,gg]], though the gg argument is red, but it works! Can you tell me why this red is safe and works? What does this red mean? why this is undocumented? where did you know about this? –  matheorem Jul 21 '13 at 9:03
    
Well, I searched some questions relate Undocumented functions. Many people suggest "don't use it". So is there any other way to realize the same thing? –  matheorem Jul 21 '13 at 10:42
    
@matheorem I added a couple of alternatives. Since you didn't explain exactly why you are using Export I'm having to guess as to your needs and I hope that PutAppend will serve. –  Mr.Wizard Jul 21 '13 at 11:34
add comment

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.