1

I have a function called doFFT.m in Octave, defined as:

function [Rx1,Rx2,fbins]=doFFT(file)

this function runs as expected returning 3 row vectors.

In Python, I have oct2py imported,

from oct2py import octave

and I call this Octave function via:

RX1,RX2,fbins=octave.doFFT(file)

I get an error that says "ValueError: not enough values to unpack (expected 3, got 1)"

As a test I redefined the function in Octave to only return 1 value, and instead called it in Python as :

RX1=octave.doFFT(file)

and this worked fine.

So, the issue seems to be with having Python understand the return format from Octave. I've tried calling the function as

[RX1, RX2, fbins]=octave.doFFT(file)
(RX1, RX2, fbins)=octave.doFFT(file)

for example, but still get either the same error.

How do I format the function call or the output from Octave, so that Python "understands" that there are 3 items being returned?

jrive
  • 218
  • 2
  • 3
  • 14
  • Try this: *response=octave.doFFT(file); print(type(response))* That should give you a clue about what's happening. Is there no documentation for the doFFT function? – DarkKnight May 12 '22 at 14:29
  • 1
    @Lancelot Thank you. the doFFT function is my own function -- I know exactly what it returns. It returns 3 row vectors. It works fine in Octave. It is only the interface with Python where I'm having trouble, where somehow python is interpreting these 3 vectors as 1. So, it is either 1) how I'm calling the function , 2) or how I must modify the returned values from the function so that python sees them as 3 vectors and not 1. – jrive May 12 '22 at 15:14
  • What does Python think the return type is? – DarkKnight May 12 '22 at 15:34
  • 2
    . I think I found out what I need to do. It is not clear to me, but it seems the octave python bridge requires me to explicitly convey the expected number of return values. If I do , RX1,RX2,fbins=octave.doFFT(file,nout=3), that seems to work. – jrive May 12 '22 at 15:35
  • 1
    @jrive yes that is correct. See [oct2py's documentation](https://blink1073.github.io/oct2py/source/api.html) for more details. This question is a near duplicate of [this one](https://stackoverflow.com/questions/43394393/oct2py-only-returning-the-first-output-argument) but I think is phrased sufficiently differently to merit not being closed as a duplicate. Please feel free to answer your own question and accept it. – Tasos Papastylianou May 13 '22 at 09:29
  • 2
    Also, for a more general explanation (e.g. addressing @LancelotduLac 's comment); the reason the number of output arguments cannot be inferred and need to be stated explicitly, is because sometimes matlab/octave functions behave differently based on the expected number of arguments. So you cannot simply get a 1x3 numpy array out and assume the 3 values correspond to that function having being called with 3 output arguments. – Tasos Papastylianou May 13 '22 at 09:29

1 Answers1

0

the oct2py python bridge requires that the expected number of returned values to be explicitly expressed in the function call, for eg.

RX1,RX2,fbins=octave.doFFT(samples, nout=3)

specifies that the function returns 3 values (or numpy arrays in this case).
When a function doesn't return anything, it must be called indicating that it returns 0 arguments, ie:

octave.foobar(in_arg, nout=0).

Thank you to @Tasos Papastlyianou for linking the oct2py's documentation in his comment.

jrive
  • 218
  • 2
  • 3
  • 14