Using Strings To Change Folders

I'm looking to do something like below. Define strings once to label paths to various folders, and then only reference those strings in other functions.

I thought I could make path0 global, but if I'm entering that as fn0("root:a:"), I don't seem to be allowed to. If I remove path0 as input into fn0, I can make it global.

Regardless of what I've done, I can get fn1 to recognize path0 without explicitly reinputting it. Any help on that front?


function   fn0(path0)

string path0

newdatafolder $path0

end



function fn1()

string path0

setdatafolder $path0

end
In both of your examples, the string variable named path0 is a local string variable. In the case of fn0(), it's a local string variable that happens to be a parameter. In the case of fn1(), there are no parameters.

If your intent is to have a global string variable named path0 that is referenced from within different functions, then you need to create a SVAR reference to that global variable within your functions and use that reference.

For example:
function fn1()
    SVAR thePath = root:path0
    setdatafolder $thePath
 end


Note that in my example, I'm hardcoding the path of the global string variable to be root:path0. This is usually the right thing to do because it will allow fn1() to produce the same results regardless of what the current data folder is. But in some cases, you might have multiple path0 global variables in different data folders, and in such cases you might want the function to look up the value of that string in the current data folder. In such cases, you could omit the root: part of the path specification.

I suggest you have a look at the following help topics, which you can get to by executing the following commands on Igor's command line:
DisplayHelpTopic "SVAR"
DisplayHelpTopic "Converting a String into a Reference Using $"
DisplayHelpTopic "Accessing Global Variables And Waves"
This helps, thank you. I'm not very familiar with SVAR, so I'll do some reading.
It seems like a string input as a parameter can't be global?

I get "Ill-formed name" when I run this:

function   fn0(path3)

string/g path3 = "root:b"

end
Also, SVAR seems like the right way to go if I want to use strings. But now I'm looking into Data Folder References. Are those preferable? I didn't see a way to make them global.

geologic wrote:
It seems like a string input as a parameter can't be global?

I get "Ill-formed name" when I run this:

function   fn0(path3)

string/g path3 = "root:b"

end


Input parameters can't be globals.

(Why would you need a global input? Just use the global with SVAR).

Try:

// save parameter as data folder path that will be returned by UseCommonDF()
function  SetCommonDF(path)
    string path // input parameter, something like "root:df"
 
    NewDataFolder/O $path // ensure the data folder exists
    String/G root:path0 = path // remember which data folder fn1 will return
end
 
 
// usage: String oldDF= UseCommonDF()
// <do stuff>
// SetDataFolder oldDF

Function/S UseCommonDF()

    String oldDF= GetDataFolder(1)
    String commonDF= StrVarOrDefault("root:path0", oldDF) // df path set by . On error, use current DF
    setdatafolder $commonDF

    return oldDF
 End


--Jim Prouty
Software Engineer, WaveMetrics, Inc.
geologic wrote:
It seems like a string input as a parameter can't be global?

I get "Ill-formed name" when I run this:

function   fn0(path3)

string/g path3 = "root:b"

end

No, when you use path3 as an input parameter, you are creating a local variable name "path0". It is not a global variable, and cannot be "converted" into global. You have to make a new global variable.

You CAN use the input parameter to set the CONTENTS of the global variable:
Function fn0(myNewPath)
    String myNewPath

    String/G path3 = myNewPath
end

Quote:
Also, SVAR seems like the right way to go if I want to use strings. But now I'm looking into Data Folder References. Are those preferable? I didn't see a way to make them global.

Use NVAR to connect to a numeric global variable, and WAVE to connect with a wave.

These keywords have two purposes, one compile-time and one run-time.

The compile-time purpose is to provide a name that you can use in your code to refer to something whose name you don't know when you write the code, or which may not exist when you write the code. That name is local- it can only be referred to in your code within the function in which it is defined. It also tells the compiler what sort of object that name refers to, so that the compiler can put on code for the right kind of object.

The run-time purpose is to actually create a pointer to the global object (string, numeric variable or wave). That is, at run-time when that line is encountered, Igor looks for the actual object in the path you specify. The local name (which, confusingly, can be the same as the global name) now is an alias for the actual object.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
JimProuty wrote:

Input parameters can't be globals.

(Why would you need a global input? Just use the global with SVAR).


It was partially just what I experienced with test functions I was making. But, I was thinking about inputting a path for a given function as a string parameter, declaring that path global, and then accessing the waves created by that function via the global string reference.


Ok, so it seems the thing to do is declare my parameters as strings (if I have parameters), then declare those strings as globals, then access those globals from other functions with SVAR. Sound right?
"Global" means global. These variables are part of the experiment; not only of the function. You can access these global objects (variables, strings, waves,...) with a local alias (by nvar, svar, wave,...).
(Regular) parameters given to a function are local and cannot be seen by the experiment and other functions.
The "/G" flag creates an object with global scope. (Non-FREE Waves are always global.)
Some example:
function   fn0(path0)
  string path0    // This is the local parameter
  String /G root:MyDataFolder  // This creates a global string
  MyDataFolder=path0 // This stores the value of the parameter in the global string
  newdatafolder $path0
end

function fn1()
  SVar MDF=root:MyDataFolder  // This established a connection to the global string
  setdatafolder $mdf  //And here it's used
end


HJ
Ok, this is my understanding for how to use global waves to switch data folders. Please correct me if there's a better way. I made a folder to store the global strings. Stylistically, is this a good idea? Or should I store each global string in root? Or maybe in the folder it is a path to (this seems problematic)?

function f0(samp)

string samp
newdatafolder/o/s root:stringglob

string/g samppath = "root:"+samp

end


function f1()

setdatafolder root:stringglob
svar samppath
setdatafolder root:

newdatafolder/o/s $samppath

end