Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In a Package I am writing, I'm trying to define a function with an Optional parameter in it that is set to a global variable.


However, the Optional value IndexLoopPlot is not set until after the package is loaded and other functions are run. Therefore PlotIndex is read in as,


In other words, IndexLoopPlotMtx is set to Null, and when the function is run later it tries to plot {} instead of the matrix IndexLoopPlot. Is there a way to get it delay setting the Default until the function is called for the first time from the package?

share|improve this question

migrated from Apr 12 '12 at 2:42

This question came from our site for people studying math at any level and professionals in related fields.

1 Answer 1

You'll have to point to the global variable using its full context path as Global`IndexLoopPlot. Otherwise, the optional variable will be interpreted as YourPackage`IndexLoopPlot. The following example shows how:

Quiet@Remove[a, "test`*"];
f[x_: Global`a] := {x}

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.