Message Boards Message Boards


How to have functionality of an include file?

Posted 4 months ago
2 Replies
4 Total Likes

I have a Mathematica notebook that sometimes uses one set of definitions and sometimes other definitions for the same function and string names. I tried putting one set of them in a notebook and then used SaveAs to create a .m file. I then tried to utilize it in the main notebook with Import and with Get but to no avail. What else is needed?



.m file contents:

distanceName = "Bray-Curtis" ;

distanceAbbr = "BC";

distanceLongName = "Bray-Curtis Individual Distance" ;

distanceFunc[X_, Y_] := 
  100*(1 - (2*((Total[
           Min[#[[1]], #[[2]]] & /@ 
            Transpose[{Flatten[X], Flatten[Y]}]])/
         (Total[Flatten[X]] + Total[Flatten[Y]]))));

distanceNorm[X_] := 1;

distanceUnits = "100BC";
2 Replies
Posted 4 months ago

Hi Richard,

In the notebook with the common definitions add the following. It will create the .m file automatically whenever the notebook is saved so you do not have to manually "Save as".

SetOptions[InputNotebook[], AutoGeneratedPackage -> Automatic]

Mark all of the cells in the notebook as initialization cells (you do not have to evaluate them). Then use Get to load the definitions into another notebook.

Thank you Rohit, this is very helpful. I did need to EVALUATE BEFORE SAVING, and then the .m file was automagically produced. Here's my test run -- I'm extremely happy!

In[1]:= X = {{1, 2, 3.2, 1.5}, {1.2, 1, 3.1, 2}};

In[2]:= Y = {{1, 1, 3.1, 1.2}, {1, 2, 3, 2}};

In[3]:= Get[
  "C:\\Users\\Authorized User\\Documents\\SSR Distance \

In[4]:= {distanceFunc[X, Y], distanceUnits}

Out[4]= {9.21502, "100BC"}

In[5]:= Get[
  "C:\\Users\\Authorized User\\Documents\\SSR Distance \

In[6]:= {distanceFunc[X, Y], distanceUnits}

Out[6]= {14.6629, "10F"}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract