Indexing within double quotation mark

Hi community,
Julia is amazing in “$a”. One step further, are there any way to achieve this?

plot(…,label=[“a1 is $a[:,1], a2 is $a[:,2].”])

Or other command like ‘legend!(a,b)’ to define which parameters will be specified in legend?

It could be done by enclosing the expression with parentheses, e.g. "a1 is $(a[:,1])".

1 Like

Thank you. It works.
plot(c,label=["a1= $(a[:,1])" , " a2=$(a[:,2])"])

An extra addition for getting >=1 parameters presented for each series in legend.
As "$(a[1,2])" cannot function with a commas betwen parameters included like "a1=$(a[1,2]), a2=$(a[2,2])" , """ """ would nail it, which is:

scatter(a,simu_mtx[a,:], label=["""s0=$(b[1,1])", "2Nμ=$(b[1,2])""" , """s0=$(b[2,1])", "2Nμ=$(b[2,2])"""])

One following question:
Compared to specify the legend by listing, I tried to use for loop.

for i in 1:2, j in 1:2
  push!(legend,"""s0=$(ndx_mtx[j,i])", "2Nμ=$(ndx_mtx[j,i])""")

#on REPL print
["s0=0.05 \",\"2Nμ=0.05 ", "s0=1.0 \",\"2Nμ=1.0 ", "s0=0.005 \",\"2Nμ=0.005 ", "s0=1.0 \",\"2Nμ=1.0 "]

The \ in printed output is unwanted. Is there a way to leave it while keeping "s0=0.05 \",\"2Nμ=0.05 " as one element pushed to legend string vector?

The \ is an escape character for the single quotes. Try adding a dot after the println command, i.e. println.(legend), and you should see that there are single quotes without backslashes.

It wasn’t clear to my why triple quotes (""") were being used. I think that the line in the loop could be:

push!(legend,"s0=$(ndx_mtx[j,i]), 2Nμ=$(ndx_mtx[j,i])")

and that should work.

It works now :slight_smile: Thank you.