Quicker Way to Overwrite Folders
geologic
string/g reffftfolderstr = "root:"+ref+":fft"
if(datafolderexists(reffftfolderstr)==1)
killdatafolder $reffftfolderstr
newdatafolderpath(reffftfolderstr)
else
newdatafolderpath(reffftfolderstr)
endif
if(datafolderexists(reffftfolderstr)==1)
killdatafolder $reffftfolderstr
newdatafolderpath(reffftfolderstr)
else
newdatafolderpath(reffftfolderstr)
endif
Is there a faster way to do this? Especially, if I have to do this to five folders in a function, is there way to make a text wave to cycle through the folders I would like to kill? Also, note that there is a unique string title, such as reffftfolderstr, being created for each.
newdatafolderpath is a function pulled off the Igor Exchange that creates data folder paths.
variable i
for (i=0;i<numpnts(RefWave);i+=1)
string/g reffftfolderstr = "root:"+RefWave[i]+":fft" // Why global? Note: RefWave[i]
if(datafolderexists(reffftfolderstr)==1)
killdatafolder $reffftfolderstr
newdatafolderpath(reffftfolderstr)
else
newdatafolderpath(reffftfolderstr)
endif
endfor
Not tested but it should work like this.
HJ
April 22, 2016 at 12:39 am - Permalink
I've been on the fence about whether or not to use global.
I have many functions that call from each other, and often I will need to pull waves from a folder different than the one I'm in. I like being able to list the svar folder names and other definitions at the beginning of a folder so that I don't have to import variables and strings into each function. I simply use function1() and have every global string, etc. defined as svar and nvar.
But, I'm a little confused as to how global these strings and variables are...If they're really global, is there a way not to have to load them into new functions each time?
April 22, 2016 at 07:53 am - Permalink
In general, you should avoid global variables. They make it hard to know how a change in one place will affect code in another place.
Use a structure instead. In a top-level function you can create a structure instance and load it up with all the info your system needs. Then pass that single object around to all the sub-functions.
DisplayHelpTopic "Runtime Lookup of Globals"
And there's a lot of good material before and after that topic.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
April 22, 2016 at 09:33 am - Permalink
displayhelptopic "Runtime Lookup of Globals"
NVar and SVar provide a connection to a global variable or string. Variable / String creates a local variable or string which is lost after the function ends. The /G flag does the same but keeps the object in memory and lists it in the data folder structure.
You string is overwritten in every iteration of the loop since you don't change the data folder (or you are in the wrong one). If you need to use many variables etc. you might want to store them together in a dedicated data folder (eg. root:MySettings) or use data structures in case you have different parameter sets for each experiment set (eg. data42016, data42116, data42216 ect.)
HJ
PS: Saw JW's post after I posted my comment.
April 22, 2016 at 09:42 am - Permalink
I'm not redefining these variables. I'm assigning folder names, results, constants one value and then leaving it. I *think* global variables will work like this even if it's not the elegant solution.
I'll look into this. I'd be adding new global variables throughout the function, so I need to make sure this is the right choice. For instance, maybe I take the input parameters and do a calculation and the result of this calculation would be used elsewhere in the function, so I would like to declare it as a global variable or part of a structure.
I'm going through the documentation now, but wanted to outline what I'm looking for in case anyone can comment in the mean time.
April 23, 2016 at 07:02 am - Permalink
I see...I need to learn about data structures...
April 23, 2016 at 07:10 am - Permalink
HJ
April 23, 2016 at 03:41 pm - Permalink
At least for the level I'm coding at, I think I can use global strings without too much headache. Regarding the structures, am I right in understanding that this can be a block of strings that I can append new strings to?
April 24, 2016 at 06:56 am - Permalink
displayhelptopic "getdatafolder"
.There is no need to store the name of a data folder in a global string in that specific data folder...
Concerning the structures: yes you could. Maybe a 'list of strings' (->stringfromlist), a text wave, or a wave of data folder references MIGHT also be applicable, depending on the actual problem.
HJ
April 24, 2016 at 07:29 am - Permalink