Take the 2-minute tour ×
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.

PlotIndex[IndexLoopPlotMtx_:IndexLoopPlot]:=ListPlot[.....

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,

PlotIndex[IndexLoopPlotMtx_:{}]:=ListPlot[.....

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 math.stackexchange.com Apr 12 '12 at 2:42

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

add comment

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`*"];
BeginPackage["test`"];
f[x_: Global`a] := {x}
EndPackage[];

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

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