4

I recently installed oct2py along with its dependencies in order to import a few matlab functions to be used in my python code. The .m file is located in the same directory as my python code.

Could you please help me figure out, how I would import those .m files along with the methods located within them and how would I use those functions within my python code, considering that the actual functions are defined in octave/matlab. I have included an example .m file with a function

function x=readfile(y)
%  Puts the contents of a text file with path and name
%  specified in string y, into char array x.
%      Example:  mystring = readfile('c:\workdir\readme.txt');


fid = fopen(y,'r');     %  Read the Plaintext
M = fread(fid);
fclose(fid);  
x = char(M');

The above function is located in a file named 'readfile.m' within the same directory as my python code.

MShakeG
  • 391
  • 7
  • 45

2 Answers2

1

This is as simple as

from oct2py import octave
octave.feval( 'readfile', 'your_input_file_here.txt' )

PS. Your octave executable needs to be available from the system path.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
0

So apparently octave 4 doesn't have 'octave.exe' located within bin, however octave 3 does, and works perfectly well with oct2py

MShakeG
  • 391
  • 7
  • 45
  • 1
    You need to provide a bit of info (are you on linux? windows? have you put the octave binary in the path? alternatively have you set the OCTAVE_EXECUTABLE environmental variable to point to the binary?). But generally speaking, octave 3 is **atrociously** old now. This is not the best solution. Find a way to make the latest octave work for you instead. – Tasos Papastylianou Feb 12 '19 at 23:04