Using Strings To Change Folders
geologic
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
string path0
newdatafolder $path0
end
function fn1()
string path0
setdatafolder $path0
end
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:
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 "Converting a String into a Reference Using $"
DisplayHelpTopic "Accessing Global Variables And Waves"
September 22, 2015 at 08:33 am - Permalink
September 22, 2015 at 08:47 am - Permalink
I get "Ill-formed name" when I run this:
string/g path3 = "root:b"
end
September 22, 2015 at 09:33 am - Permalink
September 22, 2015 at 09:35 am - Permalink
Input parameters can't be globals.
(Why would you need a global input? Just use the global with SVAR).
Try:
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.
September 22, 2015 at 11:02 am - Permalink
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:
String myNewPath
String/G path3 = myNewPath
end
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
September 22, 2015 at 11:04 am - Permalink
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?
September 22, 2015 at 08:52 pm - Permalink
(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:
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
September 23, 2015 at 03:14 am - Permalink
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
September 23, 2015 at 06:14 am - Permalink