1

I have a script that performs a lot of computation on startup, and defines some functions, and I want to ensure it is all loaded when I run other scripts. I'm running Octave 5.1.0 configured for x86_64-w64-mingw32.

I initially tried call the script only if one of its variables didn't exist('varName'). That doesn't seem to work, so I marked a lot of the variables global, but they still aren't loaded into global, and the functions aren't either. The script works well if I call it from the top level of the REPL or the other script's top level (but not inside an if test). I also checked out other questions like octave: load many functions from single file.

This is what I want to do:

global all;
if (!exist('all'))
  prepareData
endif

I am wondering how to expose the global variables and functions in the prepareData script only the first time it is run.

1 Answers1

1

After going through the question writing process, it became clear to me that I can simply put the if (!exist('all')) into the original script to prevent it from loading the data multiple times. So now in the prepareData script I just put my expensive data loading inside this:

global all;
if (!exist('all') || length(all) < 1);
  # Expensive load calls
endif

and in the consuming script I can call simply:

prepareData;

# Happily use data