计数gnuplot中的csv中的列 [英] Count columns in csv in gnuplot
问题描述
gnuplot中是否有一个函数可以返回csv文件中的列数? 我在文档中找不到任何内容,也许有人可以为此建议自定义功能?
Is there a function in gnuplot which returns the number of columns in a csv file? I can't find anything in the docs, maybe someone can propose a custom made function for this?
推荐答案
从gnuplot4.6开始,您可以编写一些hack脚本来执行此操作.它当然不是最有效的,但它是纯gnuplot:
As of gnuplot4.6, you can make a little hack script to do this. It is certainly not the most efficient, but it is pure gnuplot:
#script col_counter.gp
col_count=1
good_data=1
while (good_data){
stats "$0" u (valid(col_count))
if ( STATS_max ){
col_count = col_count+1
} else {
col_count = col_count-1
good_data = 0
}
}
现在在您的主脚本中,
call "col_counter.gp" "my_datafile_name"
print col_count #number of columns is stored in col_count.
这有一些局限性-如果数据文件中的一列完全是非数字的,然后再加上更有效的列,这将使您感到窒息,但是我认为它应该适用于许多典型的用例.
This has some limitations -- It will choke if you have a column in the datafile that is completely non-numeric followed by more valid columns for example, but I think that it should work for many typical use cases.
print col_count
最后,您可以使用环境变量GNUPLOT_LIB
,然后甚至不需要在当前目录中使用col_counter.gp
.
As a final note, you can use the environment variable GNUPLOT_LIB
and then you don't even need to have col_counter.gp
in the current directory.
这篇关于计数gnuplot中的csv中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!