Oct2Py only returning the first output argument
Asked Answered
S

1

11

I'm using Oct2Py in order to use some M-files in my Python code. Let's say that I have this simple Matlab function :

function [a, b] = toto(c);
    a = c;
    b = c + 1;
end

What happens if I call it in Octave is obviously :

>> [x,y] = toto(3)
x = 3
y = 4

Now if I call it in Python, using oct2py :

from oct2py import octave
my_dir = "D:\\My_Dir"
octave.addpath(my_dir)
a,b = octave.toto(3)

This returns :

TypeError: 'int' object is not iterable

It seems like octave.toto(n) only returns the first value, when I'd expect two... Can anyone explain to me what I should be doing ? Thanks

Samy answered 13/4, 2017 at 14:3 Comment(0)
J
12

In older versions of Oct2Py (3.x and older), the number of output arguments was inferred from the call within Python, so if you wanted multiple outputs, you would simply request both outputs

a, b = octave.toto(3)

However, as of version 4.0 you now need to use the nout kwarg to your function call to explicitly specify the desired number of output arguments

a, b = octave.toto(3, nout=2)

From the 4.0 Release Notes

Removed inferred nout for Octave function calls; it must be explicitly given if not 1. The old behavior was too surprising and relied on internal logic of the CPython interpreter.

Jalap answered 13/4, 2017 at 14:34 Comment(5)
I understand the difference ! However this line returns : TypeError: 'int' object is not iterable. Exactly as if I wrote : [a,b] = octave.test(3) ... It really looks like octave.test(n) gives a single output...Samy
@Samy It definitely should work. Can you change your function name to something that doesn't conflict with a core library function? Also, please show the exact code that you're trying to useJalap
I changed my function's name, there's no longer a conflict but still the TypeError. I can share my exact code lines if needed but I'm really testing with the simple ones above and it truly does not work.Samy
Why is it incorrect ? I mean, after I replaced the print command with "a,b ="... I have Octave version 4.2.1, and oct2py 4.0.6, just installed a few days ago.Samy
@Samy OH! Sorry, this was a change introduced in version 4.0. See update.Jalap

© 2022 - 2024 — McMap. All rights reserved.