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 used to do a lot of programming in R and recently switched to Mathematica. One thing that bothers me is that I haven't found a way to save variables, their values and definition!

Let's look at an example:

idx = Partition[RandomSample[Range[20], 20], 3];
Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1, 
Length[idx]}];
Subscript[vars,{9,15,8}]
(*32*)

Is there a way to save the variable names as well as the corresponding values. And how would I read them into a new file? So that in a new file I can access with Subscript[vars,{9,15,8}] the value.

I tried it with Table[...]>>values.mx and read it in with values=<<values.mx but that only gives me the values, whereas I also need the definitions. I also tried:

DumpSave["values.mx", 
 Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1, Length[idx]}]]

but got an error

DumpSave::bsnosym: "Table[..] is not defined as a symbol or a context"

share|improve this question
1  
Does DumpSave[ ] not work properly for you? –  bill s Mar 15 '13 at 8:22
1  
@bills In the last sentence the OP said that he can't get the proper syntax for DumpSave[]. Can you help him? –  belisarius Mar 15 '13 at 8:50
    
yes, I didn't geht the syntax right, I tried DumpSave["values.mx", Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1, Length[idx]}]] but got an error (*DumpSave::bsnosym: "Table[..] is not defined as a symbol or a context"*) –  rainer Mar 15 '13 at 9:39
    
Hi rainer, you are fighting the syntax here: Subscript[vars,i] is not the easy way to do indexing in Mathematica. It is more of a typesetting convenience, that is, for display. The easy thing to do is to have vars be a list, and to index into that list. This is the way I answered the question below. –  bill s Mar 15 '13 at 10:00
    
@Mr. Wizard isn't that post about saving a whole function definition to a file? Whereas here I'm trying to save variable name based on subscript. I already read that post and found out about DumpSave. –  rainer Mar 15 '13 at 11:06
show 2 more comments

2 Answers 2

up vote 5 down vote accepted

There are two easy ways I can think of to do this, but both require a change to your methods.

The first is to merely use indexed objects instead of Subscript, e.g. vars[1] = x; vars["thing"] = y; . . .. You would then use Save["file.m", vars].

The second will allow you to use Subscript but you will need to change the way the definitions are made, to attach the definitions to the symbol vars rather than Subscript. (Described here.)

vars /: Subscript[vars, n] = . . .

This creates UpValues on the symbol vars which will be saved by Save["file.m", vars].


A complete working example:

idx = Partition[RandomSample[Range[20], 20], 3];

Table[vars /: Subscript[vars, i] = Total[i], {i, idx}];

Save["vars-test.m", vars];

Now check what was saved:

FilePrint["vars-test.m"]

For additional (general) methods of saving definitions see this post.

share|improve this answer
    
@rainer I'm certainly glad we got that figured out. Thanks for the Accept. –  Mr.Wizard Mar 17 '13 at 13:18
add comment

Start with your data idx. What you are calculating is the Total of each of the triplets in idx. Let's do this the easy way and assign it to a variable

vars = Map[Total, idx]

You can save vars to a file like this:

DumpSave["test.mx", vars]

Then, after quitting and starting again, you can get the saved variables and data back by

<<test.x

Now, if you want the nth entry of vars, you type

 vars[[n]]
share|improve this answer
    
Thanks, but actually it's more difficult than that. I apply another more complicated function. And I do need this specific output because it's an input somewhere else –  rainer Mar 15 '13 at 10:12
    
You've seen the new integration with R? wolfram.com/mathematica/new-in-9/built-in-integration-with-r There's a whole page on how to translate data from Mathematica to R (and back): reference.wolfram.com/mathematica/RLink/tutorial/DataTypes.html –  bill s Mar 15 '13 at 10:19
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.