Setting a Data Folder Path in a user-defined function

Hi,

I am trying to use the SetDataFolder command in a userdefined function, so that I may get to the waves I create with it from other functions.
However, I get the following error message :

Function RelativeKayserCalc(Rel,X0)
SetDataFolder root:Data:Load
Variable Rel,Xpnts,i=0
wave X0
wave XK

Xpnts = numpnts(X0)

do
XK[i]=CalcStokes(X0[i],Rel)
i=i+1
while(i<Xpnts)

End


This function runs after being called by a top level routine that creates the new data folder paths.

Is there a way around this problem?
I have the impression there should be an easy solution, but I've tried to no avail.

Thanks,
Nicolas
You need to declare the types of the function arguments (Rel and X0) as the first thing in the function body.
Just as an addition: You don't need SetDataFolder just to reference waves (see example below), as you can just reference you waves directly. SetDataFolder is rather useful if you want to create large sets of data within the function. Also, if you plan to use SetDataFolder, I would suggest to set the folder back at the end. Otherwise you might mess up other functions, when the set folder is wrong. Here is a rewritten example:

Function RelativeKayserCalc(Rel,X0)
Variable Rel    // first, always declare your input
Wave X0

Variable i  // keep in-func declarations separate
Wave workwave = root:Data:Load:XK   // reference the wave directly without setdatafolder

for (i = 0; i < numpnts(X0); i+=1)  // I would use a for loop here, but that's just my taste
    workwave[i]=CalcStokes(X0[i],Rel)
endfor
End
Hi all,

Thanks a lot for helping me out.
I had a feeling it was going to be an easy answer :).
I still have a little work ahead of me!

Nicolas
For longer functions that access the same folder with different waves, a more elegant solution is to use DREF + SDFR statements. Also, you might be able to avoid a for ... endfor loop with proper use of the p notation for wave indexing.

Function RelativeKayserCalc(Rel,X0)
    variable Rel
    wave X0

    DFREF mydatafolder = root:Data:Load
    wave/SDFR=mydatafolder workwave = XK
    workwave=CalcStokes(X0[p],Rel)
end


For this specific short case, I would just eliminate the call to RelativeKayserCalc(...) in the main function and replace it directly by its code. You may have other things going on in the entire function though that make sense for it to be a full function.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
My preferred way to do this is:
Function RelativeKayserCalc(Rel,X0)
    Variable Rel
    Wave X0
 
    DFREF dfr = root:Data:Load
    Wave XK = dfr:XK
    XK=CalcStokes(X0[p],Rel)
End


The /SDFR flag is supported only in WAVE, NVAR and SVAR statements.

The dfr: syntax shown above is supported wherever Igor expects a wave or global variable. This includes WAVE, NVAR and SVAR statements and in any command that uses a wave, for example, in a Display command.

Often dfr will be passed as a parameter to identify the data folder in which the function is to work.
hrodstein wrote:
My preferred way to do this is: ..


Ah yes ... that is cleaner for sure.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville