Reputation: 6891
to make the thing clear:
i have data that looks like this:
0, , ,1,2
1, , ,2,3
2, 1, ,4,5
3, 1, ,4,6
4, 1,1,4,7
5, 1,2,5,8
6, 1,4,6,7
multiple columns with missing values (only at the begin of the file). now i want to plot, column 2,3,and 5 but the plots should start at the point where all the sets i want to plot have data.
so if my xrange is [0:10] only from 4:6 should be plotted if i plot 2,3,5. If i plot 2,4,5 then from 2:6 should be plotted. is there some possibility to test if a value exists?
i allready tried something like
plot \
"file.txt" using 1:($2 + ($3*0) + ($5*0)) with lines, \
"file.txt" using 1:($3 + ($2*0) + ($5*0)) with lines, \
"file.txt" using 1:($5 + ($3*0) + ($2*0)) with lines, \
because i hoped if the data of one element to use is not present then gnuplot will ignore the whole line.
Somebody an idea?
Upvotes: 2
Views: 1274
Reputation: 6891
ahhh - i misinterpreted my output.
Now if i test the whole thing on the minimal example i posted above, my solution seems to work.
i now created some check function dummys, to made things a bit nicer.
check2(x1, x2) = 0
check3(x1, x2, x3) = 0
check4(x1, x2, x3, x4) = 0
check5(x1, x2, x3, x4, x5) = 0
check6(x1, x2, x3, x4, x5, x6) = 0
check7(x1, x2, x3, x4, x5, x6, x7) = 0
check8(x1, x2, x3, x4, x5, x6, x7, x8) = 0
plot \
"file.txt" using 1:($2 + check3($2, $3, $5)) with lines, \
"file.txt" using 1:($3 + check3($2, $3, $5)) with lines, \
"file.txt" using 1:($5 + check3($2, $3, $5)) with lines, \
however if somebody knows a better way, please share your knowledge.
thank you
in combination with macros that Christoph suggested in the other answer the whole thing is more compact:
set macro
check3(x1, x2, x3) = 0
AllOrNothing = "check3($2, $3, $5)"
plot \
"file.txt" using 1:($2 + @AllOrNothing ) with lines, \
"file.txt" using 1:($3 + @AllOrNothing ) with lines, \
"file.txt" using 1:($5 + @AllOrNothing ) with lines
Upvotes: 1
Reputation: 48390
Basically, you can use the valid
function to check if a column contains valid data. This returns 1
if the data is valid, 0
otherwise. So a possible plot
command looks like this:
plot 'file.txt' using 1:(valid(2)+valid(4)+valid(5) == 3 ? $2 : 1/0),\
...
Following is a variant, where the columns to use are defined in a string cols
. The valid
check is constructed dynamically and invokes as macro later in the using statement.
set datafile separator ','
set style data lines
set macros
cols = "2 4 5"
col_cnt = words(cols)
col_valid = ""
do for [i in cols] {
col_valid = col_valid . sprintf("valid(%s)+", i)
}
col_valid = col_valid . '0 != col_cnt ? 1/0'
set offsets 0,0,1,1
plot 'file.txt' using 1:(@col_valid : $2) t 'col 2',\
'' using 1:(@col_valid : $4) t 'col 4',\
'' using 1:(@col_valid : $5) t 'col 5'
You could of course also include looping over the cols
values. I have skipped that part so retain some readability ;)
.
EDIT: Improved the script according to @vlad_tepesch's comment.
Upvotes: 1