blink1073 / oct2py

Run M Files from Python - GNU Octave to Python bridge
http://blink1073.github.io/oct2py/
MIT License
261 stars 54 forks source link

Returning class object from Oct2Py #174

Open no-clue-what-to-do opened 4 years ago

no-clue-what-to-do commented 4 years ago

I'm trying to run a basic MATLAB script that defines a class, and get that class object returned to python. I don't know MATLAB well, and am very new to Oct2Py so I may be completely misunderstanding how to do this. Any help would be greatly appreciated.

Here is the Matlab file (taken from here)

classdef BasicClass
   properties
      Value {mustBeNumeric}
   end
   methods
      function r = roundOff(obj)
         r = round([obj.Value],2);
      end
      function r = multiplyBy(obj,n)
         r = [obj.Value] * n;
      end
   end
end

And I call this in the python script with the following

from oct2py import octave
octave.addpath(r'C:\Users\i13500020\.spyder-py3\IST')
oclass = octave.class_example(nout=1)

when I run this I get a warning that prints four times and then an error message

First: warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties. All properties are returned, including private and protected ones.

And then: TypeError: 'NoneType' object is not iterable

I don't have any trouble running the roundtrip example from the Oct2Py page, so I know my installation is fine

yasirroni commented 4 years ago

I don't think oct2py support calling .m class as python object. What you can do is make your .m file as function with input and output. Then, use oct2py to pass an input and fetch the output. See conversion of data between python and octave.