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?


1 Answer 1


I'm going to do my best to interpret your question and provide a useful answer. I'm having to guess regarding MapIndexed as your 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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