3
$\begingroup$

I have some parameters defined in my notebook as, for example,

a=1;
b=2;
c=3;

and I want to export these parameters to a .txt file in which I have the following text "a=1 b=2 c=3"

It doesn't matter the format I just want to have the structure "name=value". I found some information in google but none of them solved my problem. Do you know how to do it?

$\endgroup$
1
  • 3
    $\begingroup$ I think Save is all you need, or did I miss something? $\endgroup$
    – Kuba
    Apr 17, 2019 at 10:58

1 Answer 1

2
$\begingroup$

As Kuba said, you can use save to put the parameters in a text file.

a = 1; b = 2; c = 3

Save[NotebookDirectory[] <> "parameters.txt", {a, b, c}]

Then test this through Clearing the values

Clear[a, b, c]

What does the txt file look like?

enter image description here

Then import and use ToExpression

Import[NotebookDirectory[] <> "parameters.txt"] // ToExpression; 

Check the values of a, b and c

{a, b, c}

(* 1, 2, 3*)

In case you/other users want to change the parameters to influence the computations, I find that using Google Spreadsheet or Excel better than text files (more structure, easier for other users).

If the parameters relate to states internal to your system, I prefer to use .wdx files

$\endgroup$
2
  • 1
    $\begingroup$ According to the documentation for Save[] from Mathematica 12, the suggested way of importing the saved data is Get[] instead of Import[]. Indeed, Import gives an error. $\endgroup$
    – And R
    Jul 3, 2020 at 9:35
  • $\begingroup$ I checked the code with MMA 12.1.1.0. Import works. With Get you don't have to use the post fix ToExpression. However, Import is great for importing other things such as URLs, Excel files etc. $\endgroup$
    – FredrikD
    Jul 3, 2020 at 9:44

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.