3
$\begingroup$

This question already has an answer here:

This might be a stupid question but I simply don't get my head around this. I'm trying to define variables and functions in different files (tried everything: nb, m, ...) and include them for use in a NoteBook (or whatever would work).

Mathematica seems to overcomplicate everything. I simply want to include code which is written in one file into another! Can't be that hard... But obviously I did not understand the very concept of using this properly.

Here's what I have so far:

This is my config.m

BeginPackage["config`"];

BranchName::usage = "Returns Branch Name";
BranchInfo::usage = "Retruns Branch Info";
PrimaryVariables::usage = "use";

Begin["`Private`"];
BranchName = "TAPIR 1D - Atmospheric Accretion around Planets";
BranchInfo = 
"Dynamical accretion of primordial atmospheres around planets.";
PrimaryVariables = {
{1, R, "MR", "Grid Equation"},
{2, m, "MM", "Poisson Equation"},
{3, rho, "MD", "Continuity Equation"},
{4, rhoAv, "MDa", "Continuity Equation (averaged density)"},
{5, e, "ME", "Energy Equation (internal gas energy density)"},
{6, u, "MU", "Equation of Motion"},
{7, J, "MJ", "Equation of Radiation-Flux Energy Density"},
{8, H, "MH", "Equation of Radiation-Flux"},
{9, uT, "MT", "Equation of Turbulent Convection"}
};

(* Protect Config Variables *)
\
(*Protect[BranchName,BranchInfo,PrimaryVariables];
Protect[R,m,rho,rhoAv,e,u,J,H,uT];*)
End[];

EndPackage[];

This then should be called from my notebook: test.nb

<< config.m;

BranchName

What is it what I don't understand?

Thanks!

Update ---

Sorry totally forgot to add the output. This is it:

In[1]:=BranchName
Out[1]=BranchName
$\endgroup$

marked as duplicate by m_goldberg, José Antonio Díaz Navas, Kuba Mar 3 '18 at 5:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ We can't read your mind, so maybe you should tell us what went wrong instead of asking us to tell you what you don't understand? There are many examples of this kind of thing online; here's one example you could look at (github.com/noahbenson/Neurotica), specifically the Installation section of the main README/page. $\endgroup$ – nben Mar 2 '18 at 16:16
  • $\begingroup$ Hi! Thank's for the link but unfortunately this is not what I was looking for. It does not really explain how to use packages. $\endgroup$ – Florian Ragossnig Mar 2 '18 at 16:30
  • 1
    $\begingroup$ Did you make all the code input cells in your package file initialization cells before you saved it? $\endgroup$ – m_goldberg Mar 2 '18 at 16:32
  • $\begingroup$ I guess not. How do I do that? When I go to Cell->Convert To I only have the options: InputForm, Raw InputForm, OutputForm, StandardForm, TraditionalForm, Bitmap $\endgroup$ – Florian Ragossnig Mar 2 '18 at 16:36
  • $\begingroup$ I do see it in the Notebook as I used the variable in order to show me the content. Like if I define a=1 and evaluate, i will get 1 as an output $\endgroup$ – Florian Ragossnig Mar 2 '18 at 16:47
4
$\begingroup$

I finally found the solution! Thanks to @m_goldberg it was simly because the cell was not formatted as initialization cell.

So if anyone followed all the tutorials but was missing out this part just select the cell(s) which contain your code and go to

Cell->Cell Properties->Initialization Cell

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.