Oct2Py gives "TypeError: cannot unpack non-iterable float object" with Psychtoolbox function returning multiple arguments
Oct2Py gives "TypeError: cannot unpack non-iterable float object" with Psychtoolbox function returning multiple arguments
我是 运行 他们网站上使用 Oct2py 包的一个非常基本的 Psychtoolbox 示例,我正面临一个问题,当我尝试 return 两个浮点值时,它看起来像:
from oct2py import octave
import sys
def ptb():
# Clear the workspace and the screen
octave.sca;
octave.close;
octave.PsychDebugWindowConfiguration;
# Here we call some default settings for setting up Psychtoolbox
octave.PsychDefaultSetup(2);
# Get the screen numbers. This gives us a number for each of the screens
# attached to our computer.
screens = octave.Screen('Screens');
# To draw we select the maximum of these numbers. So in a situation where we
# have two screens attached to our monitor we will draw to the external
# screen.
screenNumber = octave.max(screens);
# Define black and white (white will be 1 and black 0). This is because
# in general luminace values are defined between 0 and 1 with 255 steps in
# between. All values in Psychtoolbox are defined between 0 and 1
white = octave.WhiteIndex(screenNumber);
black = octave.BlackIndex(screenNumber);
# Do a simply calculation to calculate the luminance value for grey. This
# will be half the luminace values for white
grey = white / 2;
# Open an on screen window using PsychImaging and color it grey.
window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey);
# Now we have drawn to the screen we wait for a keyboard button press (any
# key) to terminate the demo.
octave.KbStrokeWait;
# Clear the screen.
octave.sca;
sys.exit()
if __name__ == "__main__":
ptb()
错误出现在第49行:
TypeError Traceback (most recent call last)
<ipython-input-1-6e0dcd049170> in <module>
47
48 if __name__ == "__main__":
---> 49 ptb()
<ipython-input-1-6e0dcd049170> in ptb()
37
38 #Open an on screen window using PsychImaging and color it grey.
---> 39 window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey);
40
41 #Now we have drawn to the screen we wait for a keyboard button press (any
TypeError: cannot unpack non-iterable float object
我正在网上寻找解决此错误的帮助,但我看到的解释很少,none 帮助了我。当我在 Octave 中执行它时效果很好,但在 Python 中我遇到了这个问题,我试图在 window 和 windowRect 之间放置括号,但它也不起作用。我该如何继续?
正在将评论转化为答案。
这里的问题是,当 oct2py 运行 在那一行时,它的行为就好像函数是用一个参数调用的一样。 Oct2py 无法从 python 调用的输出数量推断出参数数量。 (显然这曾经是旧版 oct2py 的情况,但现在不是了:参见 )。
正如链接的答案所暗示的那样,执行此操作的方法是将额外的 nout=2
参数传递给函数。 IE。你的电话(假设所有其他参数都正确?)应该是:
window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey, nout=2);
或者,您可以 运行 您的函数使用更自然的八度语法中的“eval”,即
octave.eval("[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);")
我注意到在这种情况下您实际上不需要使用生成的输出变量,但如果您这样做了,您可以使用 oct2py 的 pull
函数将它们检索到您的 python 会话中。
我是 运行 他们网站上使用 Oct2py 包的一个非常基本的 Psychtoolbox 示例,我正面临一个问题,当我尝试 return 两个浮点值时,它看起来像:
from oct2py import octave
import sys
def ptb():
# Clear the workspace and the screen
octave.sca;
octave.close;
octave.PsychDebugWindowConfiguration;
# Here we call some default settings for setting up Psychtoolbox
octave.PsychDefaultSetup(2);
# Get the screen numbers. This gives us a number for each of the screens
# attached to our computer.
screens = octave.Screen('Screens');
# To draw we select the maximum of these numbers. So in a situation where we
# have two screens attached to our monitor we will draw to the external
# screen.
screenNumber = octave.max(screens);
# Define black and white (white will be 1 and black 0). This is because
# in general luminace values are defined between 0 and 1 with 255 steps in
# between. All values in Psychtoolbox are defined between 0 and 1
white = octave.WhiteIndex(screenNumber);
black = octave.BlackIndex(screenNumber);
# Do a simply calculation to calculate the luminance value for grey. This
# will be half the luminace values for white
grey = white / 2;
# Open an on screen window using PsychImaging and color it grey.
window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey);
# Now we have drawn to the screen we wait for a keyboard button press (any
# key) to terminate the demo.
octave.KbStrokeWait;
# Clear the screen.
octave.sca;
sys.exit()
if __name__ == "__main__":
ptb()
错误出现在第49行:
TypeError Traceback (most recent call last)
<ipython-input-1-6e0dcd049170> in <module>
47
48 if __name__ == "__main__":
---> 49 ptb()
<ipython-input-1-6e0dcd049170> in ptb()
37
38 #Open an on screen window using PsychImaging and color it grey.
---> 39 window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey);
40
41 #Now we have drawn to the screen we wait for a keyboard button press (any
TypeError: cannot unpack non-iterable float object
我正在网上寻找解决此错误的帮助,但我看到的解释很少,none 帮助了我。当我在 Octave 中执行它时效果很好,但在 Python 中我遇到了这个问题,我试图在 window 和 windowRect 之间放置括号,但它也不起作用。我该如何继续?
正在将评论转化为答案。
这里的问题是,当 oct2py 运行 在那一行时,它的行为就好像函数是用一个参数调用的一样。 Oct2py 无法从 python 调用的输出数量推断出参数数量。 (显然这曾经是旧版 oct2py 的情况,但现在不是了:参见
正如链接的答案所暗示的那样,执行此操作的方法是将额外的 nout=2
参数传递给函数。 IE。你的电话(假设所有其他参数都正确?)应该是:
window, windowRect = octave.PsychImaging('OpenWindow', screenNumber, grey, nout=2);
或者,您可以 运行 您的函数使用更自然的八度语法中的“eval”,即
octave.eval("[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);")
我注意到在这种情况下您实际上不需要使用生成的输出变量,但如果您这样做了,您可以使用 oct2py 的 pull
函数将它们检索到您的 python 会话中。