How can we concatenate a string with a variable number included?
Barbara
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
To bridge the gap between them you need to use special conversion functions:
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
In that case you will need to look into the
$
operator. ExecuteDisplayHelpTopic "Converting a String into a Reference Using $"
. This can be confusing. However, consider this: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.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.November 10, 2011 at 02:33 am - Permalink
November 10, 2011 at 02:05 am - Permalink
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
November 10, 2011 at 09:55 am - Permalink