How do I access a specific value from a wave and take this one to appear in the legend?

I have a probably really easy-to-answer question, but I couldn't find an answer by myself (hard to get good keywords so a search gives the desired answer).

I have series of data files where one is varied as a function of the other and a second one is kept fixed fro one measurement (one file). This second parameter varies only from one file to the next. All three values are written in a tab-separated text-file

eg.
A B C
-3.151500E-9 -2.574253E-8 -1.000000E-1
-2.913400E-9 -2.241544E-8 -1.000000E-1
-2.703300E-9 -1.972218E-8 -1.000000E-1

So I measure B with respect to A, and C is the same for this file but will be different for the next file.

In the graph I want now to have the value of C to appear as the legend. So fare I tried to define the wave accordingly by naming the string strIsd accordingly sprintf strIsd "V\Bg\M =%.0W1PV"+strIsd ,wave2[0]
I don't know if this is the right way to access a number of a wave. But my procedure seemed to work fine first, but now I get an error in the line         duplicate /O wave1, $strIsd saying "expected wave name".
So I suppose my way of doing it is already not so nice and it then works only in some cases.
I don't see any specific differences in the files which would indicate any reason that the error occurs on these files but not on other.

Thanks for any help


macro loadVsd_varied()
    string /G path
    string /G head

    variable    num
    string  fname
    string  strVg
    string  strIsd
    string  strVsd
    string  nummer
    variable    strlength
   
    display
    LoadWave/G/D/W/A/C
    fname=RemoveEnding(S_fileName,".txt")
    strlength = strlen(fname)
    num=str2num(fname[strlength-4,strlength])
    head=fname[0,strlength-5]
    path=S_path


    do
        sprintf  nummer "%*.*d" 1, 4, num
        fname = path + head + nummer + ".txt"

        strVg = "wv"+head+"Vg" + nummer
        strIleak = "wv"+head+"Ileak" + nummer
        strVsd = "wv"+head+"Vsd" + nummer

        strIsd = " "+head+ nummer
        sprintf strIsd "V\Bg\M =%.0W1PV"+strIsd ,wave2[0]

        duplicate /O wave0, $strVg
        duplicate /O wave1, $strIsd
        duplicate /O wave2, $strVsd
        killwaves wave0, wave1, wave2
           
        append $strIsd vs $strVg

        num += 1

        LoadWave/G/D/W/A/C fname
    while(V_flag>0)
    Legend
end
It came to my mind that the name just became too long. Does somebody know what the maximum allowed length is?
tatj wrote:
It came to my mind that the name just became too long. Does somebody know what the maximum allowed length is?

Wave names, as well as other Igor object names, can be up to 31 characters in length. But I think your problem is (also?) that your wave name contains characters such as a space and equals sign that require you to use liberal names, and to use liberal names you have to enclose the name in single quotes.

Execute the following commands on Igor's command line for more information:
DisplayHelpTopic "Liberal Object Names"
DisplayHelpTopic "Programming with Liberal Names"


Your life will be much easier if you refrain from ever using liberal wave and object names if that's possible.
The maximum length of a name is 31 characters.

Keep in mind that the error sometimes occurs in the line preceding the line that is highlighted when Igor reports the error.

By writing macros instead of functions, you are limiting what you can do in Igor programming, limiting execution speed, and not benefitting from compile-time checking of syntax. You can't use structures, switch statements, pass-by-reference parameters, for loops, free waves, data folder references and many other features. Also, all new features will be added to functions so the list of what you can not use will grow. So I recommend that you use functions instead of macros. Below is your macro rewritten as a function with comments indicating what I changed.

Also, it is better to pass parameters to a procedure rather than relying on global variables. I would pass the pre-existing waves (wave0, wave1, wave2) as parameters and also the strleak string variable which appears to be a global string variable. Or perhaps strleak does not need to be a global at all and should be a local variable.

To learn about functions, execute:
DisplayHelpTopic "User-Defined Functions"


Function loadVsd_varied()
    string /G path
    string /G head
 
    variable    num
    string  fname
    string  strVg
    string  strIsd
    string  strVsd
    string  nummer
    variable    strlength
 
    display
    LoadWave/G/D/W/A/C
    fname=RemoveEnding(S_fileName,".txt")
    strlength = strlen(fname)
    num=str2num(fname[strlength-4,strlength])
    head=fname[0,strlength-5]
    path=S_path
 
    Wave wave0, wave1, wave2        // *** Added this - declare waves
   
    do
        sprintf  nummer "%*.*d" 1, 4, num
        fname = path + head + nummer + ".txt"
 
        strVg = "wv"+head+"Vg" + nummer
       
        SVAR strIleak   // *** Added this - declare global string variable
        strIleak = "wv"+head+"Ileak" + nummer
       
        strVsd = "wv"+head+"Vsd" + nummer
 
        strIsd = " "+head+ nummer
        sprintf strIsd "V\Bg\M =%.0W1PV"+strIsd ,wave2[0]
 
        duplicate /O wave0, $strVg
        duplicate /O wave1, $strIsd
        duplicate /O wave2, $strVsd
        killwaves wave0, wave1, wave2
 
        AppendToGraph $strIsd vs $strVg // *** Changed append to AppendToGraph
 
        num += 1
 
        LoadWave/G/D/W/A/C fname
    while(V_flag>0)
    Legend
end

Thanks for the answers.
Would there also be another/easier way to define the legend, then by using the wave names?
hrodstein wrote:

By writing macros instead of functions, you are limiting what you can do in Igor programming, limiting execution speed, and not benefitting from compile-time checking of syntax.


What is then the actual difference between macros and functions? If you say I would better use functions, what are then macros for?
tatj wrote:
hrodstein wrote:

By writing macros instead of functions, you are limiting what you can do in Igor programming, limiting execution speed, and not benefitting from compile-time checking of syntax.


What is then the actual difference between macros and functions? If you say I would better use functions, what are then macros for?

Execute DisplayHelpTopic "Comparing Macros And Functions" in Igor to bring up the documentation that details the differences. There is a bullet list in that topic. The summary is that in almost every way, functions are superior to macros. Therefore, unless you require a macro (such as a window recreation macro), write the code as a function instead.

Macros existed before functions, so for backwards compatibility they still exist.