1

Is it possible to iteratively generate datablocks, where the name of the datablock is build up inside the loop? Let's assume I have three fruits (in reality there are more):

array namelist[3] = ['apple', 'banana', 'pineapple']

I want to create three datablocks with the names $apple_data, $banana_data and $pineapple_data, so I tried the following:

do for [i=1:|namelist|] {
set table '$'.namelist[i]."_data"
plot ...
unset table
}

Unfortunately, instead of datablocks gnuplot created files with these names in the working directory. I guess gnuplot is checking whether the first character after set table is a $?

My second attempt was to remove the apostrophes around $:

set table $.namelist[i]."_data"

But this raised the weird error "Column number or datablock line expected", pointing at the period right after $.

Any ideas on how to achieve that? The reason for all this is that I read in the banana/apple data files with a lengthy path, apply some lengthy calculations within using, and reuse these for lots of successive stats and plot commands. I would like to avoid having to hard-code and copy-paste the same long path and the cumbersome using command over and over again.

1 Answer 1

2

Not sure if I fully understood your detailed intention. If you only want to avoid typing (or copy pasting) a lengthy path again and again, simply use variables:

FILE1 = 'C:/Dir1/SubDir1/SubSubDir1/SubSubSubDir1/File1' 
FILE2 = 'C:/Dir2/SubDir2/SubSubDir2/SubSubSubDir2/File2'
plot FILE1 u 1:2, FILE2 u 1:2

Anyway, you asked for dynamically generated datablocks. One way which comes to my mind is using evaluate, check help evaluate. Check the example below as a starting point, which can probably be simplified.

Code: (simplified thanks to @Eldrad's comment)

### dynamically generate some datablocks
reset session

myNames   = 'apple banana pineapple'
myName(i) = word(myNames,i)

N = words(myNames)
set samples 11

do for [i=1:N] {
    eval sprintf('set table $%s_data',myName(i))
        plot '+' u 1:(rand(0)) w table
    unset table
}

plot for [i=1:N] sprintf('$%s_data',myName(i)) w lp pt 7 ti myName(i)
### end of code

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, eval does the trick. In my code above it is sufficient to change set table '$'.namelist[i].'_data' to eval 'set table $'.namelist[i].'_data' and then everything works; the plot command can be written iteratively as well: plot for [i=1:|namelist|] '$'.namelist[i].'_data' w lp pt 7. My intention is to later label minima/maxima, draw lines from the maximum of one data set to the minimum of an other data set and so on, (with extensive usage of stats), while keeping the code short.
@Eldrad yes, thanks for your suggestion, it can be simplified. I adjusted my answer in a similar way.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.