0
$\begingroup$

I have a code in which I have to use some functions and lists and use them in the body of the code. If I want to type all of them in a file in Mathematica, that file becomes very very long. For example, I have

Func1[x1_,x2_,x3_,x4_,x5_]:=f(x1,x2,x3,x4,x5);
Func2[x1_,x2_,x3_,x4_,x5_]:=f(x1,x2,x3,x4,x5);
Func3[x1_,x2_,x3_,x4_,x5_]:=f(x1,x2,x3,x4,x5);
Func4[x1_,x2_,x3_,x4_,x5_]:=f(x1,x2,x3,x4,x5);
Func5[x1_,x2_,x3_,x4_,x5_]:=f(x1,x2,x3,x4,x5); ...

List1={...};
List2={...};
List3={...};
List4={...};
List5={...}; ...


the body of my code...

I have to use them these functions and lists in the body of my code. Is it possible to type those functions and lists in a separate file and call them in the body of the code then use them?

$\endgroup$
3
  • 2
    $\begingroup$ lookup Save,DumpSave and Get. $\endgroup$
    – kglr
    Sep 22, 2018 at 18:21
  • $\begingroup$ If you have the expressions already in another notebook, then you can just evaluate that notebook using NotebookEvaluate. $\endgroup$
    – bill s
    Sep 22, 2018 at 18:27
  • $\begingroup$ Could you please give me a simple code as an example? $\endgroup$
    – AYBRXQD
    Sep 22, 2018 at 18:35

1 Answer 1

6
$\begingroup$

Say you have a notebook called "stuff.nb". In my case, I made one that has a single line:

a=100;

Now in another notebook (located in the same directory as stuff.nb), evaluate:

NotebookEvaluate[NotebookDirectory[] <> "stuff.nb"]

Everything in stuff.nb is evaluated and so $a$ takes on the value $100$. If you are not in the same directory, then you would need to use the correct path to get to stuff.nb

$\endgroup$
3
  • $\begingroup$ Is there a way to hide this code in a notebook so it executes but are not shown at print out? I would like to have the PDF clean of "initiations" and only relevant computations for the reader to see. $\endgroup$
    – mf67
    Sep 23, 2018 at 2:46
  • $\begingroup$ Thanks dear @bill s $\endgroup$
    – AYBRXQD
    Sep 23, 2018 at 4:21
  • 1
    $\begingroup$ @mf67 -- Place it in an initialization cell and close the cell. Then it will be executed when you first run something in the new notebook. $\endgroup$
    – bill s
    Sep 23, 2018 at 14:28

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.