0

I have a matlab file (.m) and I want to run this file using python. I do not have matlab on my ubuntu system. Can I still run the matlab file?

isomonotone.m

% Checks vector v for monotonicity, and returns the direction (increasing,  
% decreasing, constant, or none).
%
% Meaning of parameter tol:
% - if tol==0, check for non-increasing or non-decreasing sequence (default).
% - if tol>0, allow backward steps of size <= tol
% - if tol<0, require forward steps of size >= tol
%
% Inputs
%  v:   vector to check for monotonicity
%  tol: see above
% 
% Outputs
%  b: a bitfield indicating monotonicity.  Can be tested as follows:
%   bitand(b,1)==true  -->  v is increasing (within tolerance)
%   bitand(b,2)==true  -->  v is decreasing (within tolerance)
%   bitand(b,3)==true  -->  v is both increasing and decreasing
%                           (i.e. v is constant, within tolerance).
% --------------------------------------------------------------------------
function b = ismonotone( v, tol )
  if ( nargin < 2 )
    tol = 0;
  end

  b = 0;
  dv = diff(v);
  if ( min(dv) >= -tol ) b = bitor( b, 1 ); end
  if ( max(dv) <= tol ) b = bitor( b, 2 ); end
end


%!test assert(ismonotone(linspace(0,1,20)),1);
%!test assert(ismonotone(linspace(1,0,20)),2);
%!test assert(ismonotone(zeros(1,100)),3);
%!test
%! v=[0 -0.01 0 0 0.01 0.25 1];
%! assert(ismonotone(v,0.011),1);
%! assert(ismonotone(-v,0.011),2);

Can I run this file using python without having matlab on my ubuntu?

sam
  • 18,509
  • 24
  • 83
  • 116
  • ...no? However, see [this question](http://stackoverflow.com/questions/9845292/a-tool-to-convert-matlab-code-to-python) for some Matlab->Python cross-compilers. Also, why do you need to run this Matlab code, when you already got a Python translation of it in your previous question? – senshin Jan 04 '14 at 06:46
  • the thing is, in my prev question was for understanding how to convert to python code. I have bunch of matlab files. I will convert all the files to python but I need to check if the output of python files same as matlab files. thats the reason why I wanted to run matlab files as well – sam Jan 04 '14 at 07:31

3 Answers3

2

You can install Octave from the Ubuntu repository (or download and install the latest - that's more work)

Starting Octave in the directory where this file is, allows me to run the %! tests, thus:

octave:1> test ismonotone
PASSES 4 out of 4 tests

In fact the presence of those %! suggests that this file was originally written for Octave. Can someone confirm whether MATLAB can handle those doctest like lines?

edit - add interactive examples

octave:1> ismonotone(linspace(0,1,20))
ans =  1
octave:2> ismonotone(zeros(1,100))
ans =  3

Or from the linux shell

1424:~/myml$ octave -fq --eval 'ismonotone(linspace(0,1,20))'
ans =  1

For someone used to running Python scripts from the shell, Octave is more friendly than MATLAB. The startup overhead is much smaller, and the command line options are more familiar. In the interactive mode, doc opens a familiar UNIX info system.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • but what if I have to see what is return value of b from those test cases? – sam Jan 04 '14 at 07:56
  • I've added a couple of examples. In Octave/Matlab, you can use a function by simply calling it, provided a file of the same name is present in the load path. The basic rule is 'one function per file', though this restriction has been loosened to some degree over the years. – hpaulj Jan 04 '14 at 18:42
1

Have you tried: 1. Small Matlab to Python compiler 2. LiberMate 3. Rewrite the code using SciPy module.

Hope this helps

Vamsi Krishna
  • 475
  • 3
  • 9
  • 22
0

Python cannot run Matlab programs natively. You would need to rewrite this in Python, likely using the SciPy libraries.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82