"Can't use $ in this way in a function"
neogin
I'm making a function to normalize my graph data, its goal is to take each trace of an active graph and divide by its max value in order to normalize it to 1.
So here it is :
<br />
<br />
Function/S NormaliserGraph()<br />
string NomDesWaves<br />
variable NbrDeWave,MaxWave,pas<br />
string NomWave,NowWaveNorm<br />
<br />
NomDesWaves=TraceNameList("", ";", 1 )<br />
NbrDeWave=ItemsInList(NomDesWaves)<br />
print NomDesWaves<br />
print NbrDeWave<br />
<br />
for(pas=0;pas<NbrDeWave;pas=pas+1) <br />
<br />
NomWave=StringFromList(pas,NomDesWaves,";") <br />
NomWave=StringFromList(1,NomWave,"'") <br />
print NomWave<br />
MaxWave=WaveMax($NomWave)<br />
print MaxWave<br />
<br />
$NomWave=$NomWave/MaxWave<br />
<br />
endfor <br />
<br />
<br />
return("1")<br />
End<br />
<br />
Function/S NormaliserGraph()<br />
string NomDesWaves<br />
variable NbrDeWave,MaxWave,pas<br />
string NomWave,NowWaveNorm<br />
<br />
NomDesWaves=TraceNameList("", ";", 1 )<br />
NbrDeWave=ItemsInList(NomDesWaves)<br />
print NomDesWaves<br />
print NbrDeWave<br />
<br />
for(pas=0;pas<NbrDeWave;pas=pas+1) <br />
<br />
NomWave=StringFromList(pas,NomDesWaves,";") <br />
NomWave=StringFromList(1,NomWave,"'") <br />
print NomWave<br />
MaxWave=WaveMax($NomWave)<br />
print MaxWave<br />
<br />
$NomWave=$NomWave/MaxWave<br />
<br />
endfor <br />
<br />
<br />
return("1")<br />
End<br />
Of couse I got the "Can't use $ in this way in a function" error for the line :
<br />
$NomWave=$NomWave/MaxWave<br />
$NomWave=$NomWave/MaxWave<br />
My question is how can I do this division as I don't knonw in advance the name of my wave ?
thank you for your input,
cheers
Gautier
for(pas=0;pas<NbrDeWave;pas=pas+1) <br />
<br />
NomWave=StringFromList(pas,NomDesWaves,";") <br />
NomWave=StringFromList(1,NomWave,"'") <br />
<br />
Wave w = $NomWave //get wave reference<br />
<br />
print NomWave<br />
MaxWave=WaveMax(w) //use wave reference here, too<br />
print MaxWave<br />
<br />
w = w / MaxWave //$NomWave=$NomWave/MaxWave<br />
<br />
endfor <br />
January 5, 2012 at 05:51 am - Permalink
if there is people interested here's the final code. I added a part to save the wave before normalization and an if part to check if the wave has already been normalized to prevent the overwrite of the saved wave.
Function/S NormaliserGraph()<br />
string NomDesWaves<br />
variable NbrDeWave,MaxWave,pas<br />
string NomWave<br />
<br />
NomDesWaves=TraceNameList("", ";", 1 )<br />
NbrDeWave=ItemsInList(NomDesWaves)<br />
print NomDesWaves<br />
print NbrDeWave<br />
<br />
<br />
for(pas=0;pas<NbrDeWave;pas=pas+1)<br />
<br />
NomWave=StringFromList(pas,NomDesWaves,";") <br />
NomWave=StringFromList(1,NomWave,"'") <br />
<br />
Wave w = $NomWave //get wave reference<br />
Duplicate /O w, $NomWave+"-AvantNorm"<br />
<br />
print NomWave<br />
<br />
MaxWave=WaveMax(w) //use wave reference here, too<br />
print MaxWave<br />
<br />
if (MaxWave==1)<br />
print "Il semble que le graph "+NomWave+" soit déjà normalisé" // Execute if condition is TRUE<br />
else<br />
Duplicate /O w, $NomWave+"-AvantNorm"<br />
w = w / MaxWave // Optionally execute if condition is FALSE<br />
endif<br />
<br />
endfor <br />
<br />
<br />
return("1")<br />
End<br />
<pre><code class="language-igor"></span>
January 5, 2012 at 07:14 am - Permalink
Hi guys!
When you use the <igor> and </igor> tags instead of the <ccode> and </ccode> tags it looks like actual Igor code (plus using Adjust Indentation from the Edit menu helps a little, too):
string NomDesWaves
variable NbrDeWave,MaxWave,pas
string NomWave
NomDesWaves=TraceNameList("", ";", 1 )
NbrDeWave=ItemsInList(NomDesWaves)
print NomDesWaves
print NbrDeWave
for(pas=0;pas<NbrDeWave;pas=pas+1)
NomWave=StringFromList(pas,NomDesWaves,";")
NomWave=StringFromList(1,NomWave,"'")
Wave w = $NomWave //get wave reference
Duplicate /O w, $NomWave+"-AvantNorm"
print NomWave
MaxWave=WaveMax(w) //use wave reference here, too
print MaxWave
if (MaxWave==1)
print "Il semble que le graph "+NomWave+" soit déjà normalisé" // Execute if condition is TRUE
else
Duplicate /O w, $NomWave+"-AvantNorm"
w = w / MaxWave // Optionally execute if condition is FALSE
endif
endfor
return("1")
End
--Jim Prouty
Software Engineer, WaveMetrics, Inc.
January 5, 2012 at 11:49 am - Permalink
I have an instance of this error that I don't understand.
Variable SampleRate,TrackLength
Variable TrackTime, t, n,TrackLengthShort
variable i=0,j=0, t_perm
variable p
String LFWaveString
Variable Speed1,Speed2
//Need to loop through all waves in folder
//Keep data folder untouched, work in analysis folder
variable WaveCount = CountObjectsDFR(::, 1 ) // waves in parent folder
for (p=0; p<WaveCount;p+=1)
LFWaveString = GetIndexedObjNameDFR(::,1,p)
Wave LFWave = ::$LFWaveString
// Get speed1 and speed2
String regExp = "([[:digit:]]+), ([[:digit:]]+)"
SVAR Speed1Str, Speed2Str
SplitString /E=(regExp) LFWaveString, Speed1Str, Speed2Str
Speed1 = $Speed1Str
Speed2 = $Speed2Str
The error is on "Speed1 = $Speed1Str". I guess this should involve some problem with runtime lookup of globals without declaring them correctly, but LFWaveString is local and it seems like substring Speed1Str should also be local, so there should be no problem. Changing String declarations to SVAR as I tried above doesn't help.
April 13, 2023 at 07:44 am - Permalink
Speed1 and Speed2 are numeric variables, not references to anything global, so the $ operator isn't what you need here. Instead, you need to do something to Speed1Str and Speed2Str to get a number from them. The easy way is str2num():
Speed2 = str2num(Speed2Str)
There are other variations on this to handle some corner cases, but str2num() probably covers 99% of cases.
Let us know if that's not what you intended.
April 13, 2023 at 09:26 am - Permalink
Thank you, that immediately fixed it. I've just reviewed the string substitution help file and I think I understand better now.
April 13, 2023 at 09:30 am - Permalink