"Can't use $ in this way in a function"

Hello everybody !

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 />



Of couse I got the "Can't use $ in this way in a function" error for the line :

<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
Change your loop to:
<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 />
<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 />


Thanks; it works perfectly !

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.

<br />
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>
neogin wrote:
Thanks; it works perfectly !

if there is people interested here's the final code.


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):

Function/S NormaliserGraph()

    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.

I have an instance of this error that I don't understand.

Function VSAnalysis()

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. 

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():

    Speed1 = str2num(Speed1Str)
    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.

Thank you, that immediately fixed it. I've just reviewed the string substitution help file and I think I understand better now.