"Assign variable": Is it possible to use variables in the name of the variable itself?

Jan Lause's Avatar

Jan Lause

04 Jan, 2013 02:34 PM

Dear MWorks-Support!

I have the following problem:

I use 4 variables (Speed_Stimulus1, Speed_Stimulus2, Speed_Stimulus3, Speed_Stimulus4) to determine the speed of 4 different stimulus-objects. Those speed-variables are to be changed during the experiment. Another variable (Speedchange_Location) determines which stimulus has to change, so it ranges from 1 to 4.
What I am looking for is a "smart" assignment operation for the change of the stimulus' speed.

Until now I use a bunch of if-loops (like "If Speedchange_Location=1, then Speed_Stimulus1 = newSpeed; If Speedchange_Location=2, then Speed_Stimulus2=newSpeed...and so on).

It would slim my whole experiment and make debugging easier, if it were possible to use the assignment operation in the following way:

Speed_Stimulus$Speedchange_Location = newSpeed

were the stimulus changed is not determined by a number of if-loops but just inserted in the variable name during the assignment operation. Is such a "combined" variable name possible? I tried several variations of the syntax above in the regular assignment-object that did not work (but on the other hand: they didn't cause any errors when I tried to run the experiment...)

Thanks for any ideas :)

Jan

  1. Support Staff 1 Posted by Christopher Sta... on 04 Jan, 2013 07:31 PM

    Christopher Stawarz's Avatar

    Hi Jan,

    Is such a "combined" variable name possible? I tried several variations of the syntax above in the regular assignment-object that did not work (but on the other hand: they didn't cause any errors when I tried to run the experiment...)

    No, that isn't supported. Your attempt to use that syntax should have resulted in an error; I'll have to figure out why it didn't.

    Unfortunately, there's really no nice way to do what you want to do. This is another situation where it'd be great to have a user-accessible array data type, so that Speed_Stimulus could be a 4-element array, and you could retrieve or set the appropriate element using Speedchange_Location as the index, e.g.

    Speed_Stimulus[Speedchange_Location] = newSpeed
    

    Sadly, that isn't an option at present.

    Cheers,
    Chris Stawarz

  2. 2 Posted by Jan Lause on 05 Jan, 2013 02:11 PM

    Jan Lause's Avatar

    Hi Chris,

    thanks for the quick response - regarding the syntax that should have thrown errors and apparently didn't: In my experiment, the syntaxvariations I tried to used looked like this:

    //"Target-Variables"//
    STIM_RDP1_Speed
    STIM_RDP2_Speed
    STIM_RDP3_Speed
    STIM_RDP4_Speed
    //"Index-Variable"//
    Speedchange_Location
    
    //4 examples of what I put in the variable-field of the assignment-operation//
    STIM_RDP$(Speedchange_Location)_Speed
    STIM_RDP($Speedchange_Location)_Speed
    STIM_RDP${Speedchange_Location}_Speed
    STIM_RDP[Speedchange_Location]_Speed
    ..
    ..
    

    ....and so on. I remember that in at least one of the cases I tried an error was thrown. It referred to a syntax-error at the position of the second bracket. [Assuming that the syntax is checked "from left to right" I then concluded that the $-char is accepted by the parser]. (Just in case you need this information to figure out why there was no error in the other cases).

    Last question:
    Is the $-char to be used in the variable-field of assignments at all? As I noticed, it returns a variables value if used in report-objects.
    (If that were possible for variable-assignments as well, one could feed the value of a string into the assignment that contains the name of he target-variable - not as "nice & shiny" as the array you were talking about, but still something).

    Have a nice weekend!
    Cheers,
    Jan

  3. Support Staff 3 Posted by Christopher Sta... on 09 Jan, 2013 09:05 PM

    Christopher Stawarz's Avatar

    Hi Jan,

    Is the $-char to be used in the variable-field of assignments at all? As I noticed, it returns a variables value if used in report-objects.

    No, $ has no special meaning in the "variable" field of assignments. Report (as well as assert) actions do some custom processing to handle variable expansion in their "message" parameter.

    However, it is an interesting idea -- a "poor man's array variable", if you will. Maybe we should consider supporting this.

    Chris

  4. Christopher Stawarz closed this discussion on 16 Jan, 2013 03:09 PM.

  5. Christopher Stawarz closed this discussion on 15 Mar, 2016 02:56 PM.

  6. Christopher Stawarz closed this discussion on 11 Sep, 2017 02:56 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac