Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有多条语句的Gnuplot函数 问题:_Gnuplot - Fatal编程技术网

带有多条语句的Gnuplot函数 问题:

带有多条语句的Gnuplot函数 问题:,gnuplot,Gnuplot,是否可以定义内定义了多个语句的函数 上下文 我想通过定义函数来自动化创建堆叠图所涉及的一些计算。特别是,我希望有这样的东西 mp_setup(bottom_margin, top_margin) = \ set tmargin 0; \ set bmargin 0; \ mp_available_height = 1.0 - top_margin - bottom_margin; \ mp_current_height = bottom_margin; mp_pl

是否可以定义内定义了多个语句的函数

上下文 我想通过定义函数来自动化创建堆叠图所涉及的一些计算。特别是,我希望有这样的东西

mp_setup(bottom_margin, top_margin) = \
    set tmargin 0; \
    set bmargin 0; \
    mp_available_height = 1.0 - top_margin - bottom_margin; \
    mp_current_height = bottom_margin;
mp_plot(plot_height) = \
    mp_plot_size = plot_height * mp_available_height; \
    set origin 0,mp_current_height; \
    set size 1,mp_plot_size; \
    mp_current_height = mp_current_height + mp_plot_size;
预期用途为:

...
set multiplot
mp_setup(0.05, 0.05)

mp_plot(1.0/3.0)
plot ...

mp_plot(2.0/3.0)
plot ...
这应该会自动地导致图被很好地堆叠起来,而不需要我计算每个图的原点和大小

问题 上面定义函数的方法不起作用,因为函数定义的解析似乎在第一次出现
时结束;但是这些分号是分隔每个语句所必需的(否则,我们有
set tmargin 0 set bmargin 0…
,这是无效的)

似乎Gnuplot也不支持任何对语句进行分组的方式(如C/C++中的
{…}
);或者至少,我从来没有遇到过

可能的解决办法 我所知道的存储多个函数并对其求值的唯一方法是使用宏:

mp_setup = "<as above>"
mp_plot = "<as above>"
这个解决方案虽然应该有效,但并不十分优雅


没有其他方法可以做到这一点吗?

不,不可能定义这样的函数。在gnuplot中,用户定义的函数不能包含
set
unset
或其他命令。只允许那些返回数值或字符串变量的表达式。在这里,可以有几个表达式,用逗号分隔:

a = 0
f(x) = (a = a + 1, a + x)
print f(1)
print f(1)
除了使用宏(
@var
)的解决方案之外,我更喜欢在函数内部构造字符串并调用
eval

set_margin(s, v) = sprintf('set %smargin at screen %f;', s, v)
set_margins(l, r, b, t) = set_margin('l', l).set_margin('r', r).set_margin('b', b).set_margin('t', t)

eval(set_margins(0.1, 0.95, 0.15, 0.98))
对于多点布局的具体情况,您也可以看到。

您可以这样做

mp_设置(底部_边距,顶部_边距)=(tmargin=0,bmargin=0,mp_可用_高度=1.0-顶部_边距-底部_边距,mp_当前_高度=底部_边距)

测试: 打印mp_设置(0.05,0.05) =>0.05

正如您所提到的,函数中的分组语句还不支持。
我希望这对您有所帮助

我在回答问题时提出了两种解决方案。一个使用与您类似的方法,但使用
eval
,另一个使用语法
设置多点布局。。。边距。。。间距…
,这是5.0版(新年发布)中的新功能。@Christoph谢谢,我将在这个multiplot中查看它;然而,我仍然想知道是否有一种在Gnuplot中使用多个语句的函数的好方法。
set_margin(s, v) = sprintf('set %smargin at screen %f;', s, v)
set_margins(l, r, b, t) = set_margin('l', l).set_margin('r', r).set_margin('b', b).set_margin('t', t)

eval(set_margins(0.1, 0.95, 0.15, 0.98))