How can we concatenate a string with a variable number included?

Hello,

I would like to change the name of a wave in a for loop. For example, something which could give me d0 at the first iteration, d1 at the second one, up to dimax where imax is the maximum number reached by the loop variable i.

I tried something like name="d"+i, where name is a string.
but it doesn't work, cause apparently it is impossible to concatenate a string with a variable number. So I have to use a switch command on i, and do it imax times in my for loop, which is very painfull...

Please, would you help me to find the way to do what I am trying to? That would really simplify my life a lot....

Thanks in advance

Barbara
Igor considers variables and strings to be entirely different things that don't mix with one another. If Igor expects a string for something, you cannot provide it with a variable, and vice versa.

To bridge the gap between them you need to use special conversion functions:
variable i
string badName = "d" + i // error: mixing string and variables
string goodName = "d" + num2str(i) // good: num2str copies the number in the variable into a string


Barbara wrote:

I would like to change the name of a wave in a for loop


In that case you will need to look into the $ operator. Execute DisplayHelpTopic "Converting a String into a Reference Using $". This can be confusing. However, consider this:

  • Imagine that "someStr" is a string.

  • If you type wave hello = someStr, Igor will look for a wave with the literal name "someStr". It will not look at the contents of the string variable.

  • But if you type wave hello = $someStr, then the $ tells Igor to look at the contents of the string to get to the name of the wave you're interested in.

  • Thank you so much, it is exactly what I needed! I had a look in the help files but I didn't find that.
    Barbara wrote:
    I had a look in the help files but I didn't find that.

    It can be hard to find if you don't know what you're looking for. You can read about this topic by executing this command on Igor's command line:

    DisplayHelpTopic "Converting a String into a Reference Using $"

    Starting at that topic, there is quite a lot of relevant material in the following sections.

    Another useful topic:

    DisplayHelpTopic "Accessing Waves In Functions"

    John Weeks
    WaveMetrics, Inc.
    support@wavemetrics.com