I saw a number of related questions to the one I'm about to post; they helped, but none of them solved my problem entirely.

Get the name of a symbol passed to a function

Obtaining the name of a variable as a string

The general question is how to get the name of something (a function, a list...). From the previous questions, I learned that you could use a function with the HoldFirst attribute:

SetAttributes[f, HoldFirst]
f[x_] := SymbolName[Unevaluated@x]

This function works well when it's input is directly the element of which you want to get the name. However, in my case, I have to use Table[] and I want to get the name of multiple time series. For example, I have three time series, ts1, ts2 ts3. I then want to produce two list plots, both with ts1 on the x axis and then respectively one that has ts2 on the y axis and another that has ts3 on the y axis. Here is the code I used:


ListPlot[ Transpose[{ts1["Values"],i["Values"]}]

 ,{i,{ts2,ts3}} ]

But the y axis labels, on both plots, are the time series objects, not their names. If I use the function f[_x] that I linked above, I get:


ListPlot[ Transpose[{ts1["Values"],i["Values"]}]

 ,{i,{ts2,ts3}} ]

Then the y axis label is just "i"... So what I need is a function that evaluates i "just enough" (forget the imprecise formulation) so that it is replaced by, let's say, ts2, but then holds the form and convert it to a symbol. I do not know how to do that.

Here are three time series if you want to try and mess around with them:




1 Answer 1


Proposed alternatives

This is a matter of controlling evaluation. You can do this with varying degrees of difficulty using things like HoldFirst, Unevaluated, etc., or you can simply avoid the problem in the first place which is what I greatly prefer.

To do this you can separate the names from the data using indexed objects, Rules, or Associations. Instead of defining ts1 = TimeSeries[. . .] you would write one of the following:

ts[1] = TimeSeries[. . .]

data["ts1"] = TimeSeries[. . .]

ts1[] = TimeSeries[. . .]

rules = {"ts1" -> TimeSeries[. . .], "ts2" -> TimeSeries[. . .]}

asc = <|"ts1" -> TimeSeries[. . .], "ts2" -> TimeSeries[. . .]|>

In each case you can freely pass around a reference (name) for the TimeSeries expression without it automatically evaluating to anything.

You would reference each of these in the respective manner:

Table[Labeled[ts[i], Row[{"ts", i}]], {i, 1, 3}]

Table[Labeled[data[name], name], {name, {"ts1", "ts2", "ts3"}}]

Table[Labeled[ts[], ts], {ts, {ts1, ts2, ts3}}]

Table[Labeled[name /. rules, name], {name, {"ts1", "ts2", "ts3"}}]

Table[Labeled[asc[name], name], {name, {"ts1", "ts2", "ts3"}}]

Self-contained examples

Let me see if I can provide a concrete example using your data.

t = {3686428800, 3686515200, 3686601600, 3686688000, 3686774400, 3686860800, \
3687033600, 3687120000, 3687206400, 3687379200, 3687465600, 3687552000, 3687638400, \
3687724800, 3687897600, 3687984000, 3688243200, 3688329600, 3688502400, 3688588800, \
3688675200, 3688761600, 3688848000, 3689020800, 3689107200, 3689193600, 3689366400, \
3689452800, 3689539200, 3689625600, 3689712000, 3689798400, 3689884800, 3689971200, \
3690057600, 3690144000, 3690230400, 3690316800};

valeurs = {{2,2,1,1.4,1.9,1.8,1.7,1.5,2,2,2,2.2,1.7,1.7,1.7,1.7,2,2,1.7,2.3,1.7,1.7,1.7,1.7,2,1.7,2.4,1.7,0.5,2,1.7,1.7,1.7,1.7,2.1,1.7,2.1,3.4},

nomsVariables = {ts1, ts2, ts3};


MapThread[(#1[] = TimeSeries[#2, {t}]) &, {nomsVariables, valeurs}];

Access and labeling of various time series as an illusration:

Labeled[#[], #] & /@ nomsVariables

If nomsVariables is a list of Strings rather than Symbols I recommend that you use the second, fourth or fifth forms shown earlier so as to avoid the need to convert to Symbols:

nomsVariables = {"ts1", "ts2", "ts3"};

MapThread[(data[#1] = TimeSeries[#2, {t}]) &, {nomsVariables, valeurs}]

Labeled[data[#], #] & /@ nomsVariables

The question at face value

To leave no stone unturned I should address the actual question as asked rather than only proposing an alternative. You can HoldForm the individual Symbols in the Table and then ReleaseHold that expression when you need it to resolve to the TimeSeries:

Table[ListPlot[Transpose[{ts1["Values"], ReleaseHold[i]["Values"]}], Frame -> True, 
  FrameLabel -> {"ts1", i}], {i, {HoldForm[ts2], HoldForm[ts3]}}]

This works for the example given but it introduces other question like how to create the list {HoldForm[ts2], HoldForm[ts3]} apart from typing it in. Typically you would start with an expression of held Symbols before they (ts2, ts3, etc.) are assigned values. Another question exists that addresses the use of this form to some degree: Elegant manipulation of the variables list Nevertheless IMHO this should not be used commonly but rather as a special purpose tool.

  • $\begingroup$ Thank you for your answer. I'm trying out what you proposed by using the indexed objects ts1[]. It is indeed much simpler than messing around with the evaluation. However, I do have another question related to these indexed objects. As I dont have 3, but around 50 times series to define, I'm trying to define them as a bunch, but I get the error message Set::write: Tag Map in (#1[]&)/@{liquide,travail} is Protected.. Here is what I tried: Map[#[] &, Symbol /@ nomsVariables[[;; 2]]] = Map[TimeSeries[#, {t}] &, valeurs[[;; 2]], {1}]. $\endgroup$
    – E Bassal
    Dec 12, 2016 at 19:42
  • $\begingroup$ Where nomsVariables contains the names of all the variables, t contains the different dates and where valeurs contains the different variables (one sublist per variable). $\endgroup$
    – E Bassal
    Dec 12, 2016 at 19:43
  • $\begingroup$ Never mind, I just added an Evaluate...: Evaluate[Map[#[] &, Symbol /@ nomsVariables[[;; 2]]]] = Map[TimeSeries[#, {t}] &, valeurs[[;; 2]], {1}]. $\endgroup$
    – E Bassal
    Dec 12, 2016 at 19:55
  • $\begingroup$ @EBassal give me a minute; I am looking at your example. If you've found a solution in the mean time that's fine. $\endgroup$
    – Mr.Wizard
    Dec 12, 2016 at 19:55
  • 1
    $\begingroup$ @EBassal Please see my update. I hope you find the examples useful, but if not it seems you found what you need. Thanks for the Accept. $\endgroup$
    – Mr.Wizard
    Dec 12, 2016 at 20:04

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.