Iteratively creating & naming numerical variables

Hello, I am trying to make a for loop in which I reference a different wave and create a new variable (with a corresponding name) in each iteration, each with specific names. [I think] I was able to do the wave part by iteratively making the string:

string waveref = "waveN2_" + num2str(i+1)

and then calling that specific wave (waveN2_1, waveN2_2, etc.) with $waveref, but I can't figure out how to iteratively make new numerical variables, with corresponding names (E_1, E_2, etc.). I'd then like to assign values to those newly-created&named variables within the loop.

Thank you!

You can't create a local variable or string with a run-time name. You *can* make global variables, though, in much the same way as a wave:

Function MakeVariables()
    String varname
    Variable i
    for (i = 0; i < 10; i++)
        varname = "MyGlobalVar"+num2str(i)
        Variable/G $varname
        NVAR v = $varname
        v = i
    endfor
end

 

As alternative I'd consider using a labelled wave:

Function MakeLabelledVarWave()
    String varname
    Variable i
   
    Make/D/N=10 W_Var
    for (i = 0; i < 10; i++)
      varname = "MyVar"+num2str(i)
      SetDimlabel 0, i, $varname, W_Var
      W_var[%$varname] = i + enoise(0.1)
    endfor
   
    edit W_Var.ld
end

Then you don't clutter up your experiments with global variables and you can make it /FREE if only needed locally.

 

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More