Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have groups of descriptive variable names and dont want to write explicit code for each group, e.g. (and this is a contrived simplification)

EnergySensorALow[1]=somefuncALow[#]& /@ lstSensorA;
EnergySensorAHigh[1]=somefuncAHigh[#]& /@ lstSensorA;
EnergySensorBLow[1]=somefuncALow[#]& /@ lstSensorB;
EnergySensorBHigh[1]=somefuncAHigh[#]& /@ lstSensorB;


I want create a function that should work as follows:

                           "EnergySensor"+sensorType+"Low[1]"="somefunc"+sensorType+"Low"[#]& /@

So the function will take an input String, e.g. "A" and then refer to an existing variable name EnergySensorALow[1], which it will set equal to an existing function somefuncALow[#]& and map this over an existing list lstSensorA

The code will also be run periodically when the sensor values update, and each time the function needs to be applied to the list and stored in the appropriate "EnergySensor" variable name. The variable index[n] makes it easy to use the data in a Manipulate and select which dataset needs to be plotted, I prefer not to add additional indexes to accomplish the functionality I need.

Any suggestions?

share|improve this question
up vote 3 down vote accepted

I'm going to do my best to interpret your question and provide a useful answer. I'm having to guess regarding MapIndexed as you function doesn't make sense to me.

Starting with this data:

Do[somefuncBOBLow[i] = Prime@i, {i, 10}];

lstSensorBOB = {6, 2, 8, 3, 1};

You could use this:

fnew[sensorType_String] :=
    lhs  = Symbol @@ {"EnergySensor" <> sensorType <> "Low"},
    rhs  = Symbol @@ {"somefunc" <> sensorType <> "Low"},
    list = Symbol @@ {"lstSensor" <> sensorType}
   MapIndexed[(lhs[#2[[1]]] = rhs[#]) &, list];


To produce these definitions:






Similarly to what I previously suggested I believe you should use another method to structure your data. You can use SubValues definitions in this form:

EnergySensorLow["BOB"][1] = 13;

This way it is easy to address a particular data group via a String key.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.