2

I have a problem.m file that has a function like this: myfun(p,m).

It does some calculation and returns the result. For testing the execution of this function I have a test.m file that looks like this.
myfun(myarg1,myarg2)

If I run this file as: octave test.m Then it returns me the correct result which looks like this : 0.38007

Now, the problem is when calling this function myfun(p,m) using python. I tried to use python library : oct2py

The python code looks like this:

import sys
import subprocess
import oct2py
import requests
from oct2py import octave

def myfun(p,m):
    octave.addpath('/mypath');
    oc = oct2py.Oct2Py()
    #res = octave.myfun(p,m nout=1);#this didn't work, hence comment
    #res = oc.myfun(p, m) #this didn't work, hence comment    
    x = oc.feval("myfun",p,m);
    print(x);

if __name__ == '__main__':
    myfun(sys.argv[1],sys.argv[2])

When i run this code as: python FileName.py arg1 arg2 (same arguments i used in test.m) , it gives me a warning message and an empty list like this :

warning: some elements in list of return values are undefined []

I am not sure what to do about this. As the function seems to be returning correct result in a correct format when using octave. but for some reason oct2py is not working.

3
  • Does your Octave function actually return an output or is it just printing it to the command window? Please see minimal reproducible example. Commented Oct 16, 2017 at 14:19
  • my octave function returns an output, as i can see ans in octave-cli like this: octave:1> mytest timeBSeuCallUI = 2.6464e-04 1.5133e+00 NaN relerrBSeuCallUI = 2.7459e-05 6.2769e-05 NaN ans = 2.6464e-04 octave:2> ans ans = 2.6464e-04 Commented Oct 17, 2017 at 6:34
  • system arguments are always strings. if you need integer inputs you should parse them as integers first Commented Oct 18, 2017 at 16:23

1 Answer 1

4

Octave code:

function result = test(problem,method)    
Methods={'MC','COS','RBF-AD','RBF-MLT'};    
if problem == 1    
    S=[90,100,110]; K=100; T=1.0; r=0.03; sig=0.15;    
    U=[2.758443856146076 7.485087593912603 14.702019669720769];    
    rootpath=pwd;    
    filepathsBSeuCallUI=getfilenames('./','BSeuCallUI_*.m');    
    par={S,K,T,r,sig};    
    [timeBSeuCallUI,relerrBSeuCallUI] = 
    executor(rootpath,filepathsBSeuCallUI,U,par)    

    tBSeuCallUI=NaN(numel(Methods),1); rBSeuCallUI=tBSeuCallUI;    
    for ii=1:numel(Methods)    
        for jj=1:numel(filepathsBSeuCallUI)    
            a=filepathsBSeuCallUI{jj}(3:3+numel(Methods{ii}));    
            b=[Methods{ii},'/'];    
            if strcmp(a,b)    
            tBSeuCallUI(ii)=timeBSeuCallUI(jj);    
            rBSeuCallUI(ii)=relerrBSeuCallUI(jj);    
        end    
    end    
end    

cd(rootpath);    
for el=1:numel(Methods)    
    if strcmp((Methods{el}),method)    
        result = tBSeuCallUI(el);  
    end
 end    
end    

Python code :

import sys    
import subprocess    
import oct2py    
import requests    

def benchop(problem,method):    
    oc = oct2py.Oct2Py()    
    res = oc.test(problem,method)    
    return res    

if __name__ == '__main__':    
    benchop(sys.argv[1],sys.argv[2]) 

The problem in the octave code as when checking the problem, it says problem==1, but python is expecting a string so the list undefined warning with no resulting value. Changing octave code from problem == 1 to problem == "1" solves the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way for me to call the function that takes arguments and outputs values without using the oct2py?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.