How can a script tell if it's running on Octave or Matlab?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How can a script tell if it's running on Octave or Matlab?

ldoolitt-2
I want to make a script run on both Octave and Matlab.  Mostly
this involves simply using the common subset of the language,
e.g., only % comments, no short circuit logical operators, etc.
I do want to make a plot or two, and I think that is best done
with conditional code.  So the question is, what conditional can
I use to choose?

One way might be to use the version, and guess that major version
4 or 5 is Matlab, major version 2 is Octave.  Along the way, I found
a difference in the default behavior of the str2num function.  Will
this be stable?

I ran the following on Matlab and  Octave:
 v=version
 vn=str2num(version)
 running_under_octave=length(vn)==0
 va=str2num(strrep(version,'.',' '))
 running_under_octave=va(1)<4

Matlab (extraneous vertical space deleted):
v = 5.1.0.421
vn = 5.1000         0    0.4210
running_under_octave = 0
va = 5     1     0   421
running_under_octave = 0

Octave (somewhat less extraneous vertical space deleted):
v = 2.0.13
vn = [](0x0)
running_under_octave = 1
va = 2   0  13
running_under_octave = 1

Comments?  Better ideas?  I'm all ears!

         - Larry Doolittle   <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

How can a script tell if it's running on Octave or Matlab?

John W. Eaton-6
On 25-Sep-1998, [hidden email] <[hidden email]> wrote:

| I want to make a script run on both Octave and Matlab.  Mostly
| this involves simply using the common subset of the language,
| e.g., only % comments, no short circuit logical operators, etc.
| I do want to make a plot or two, and I think that is best done
| with conditional code.  So the question is, what conditional can
| I use to choose?

You could look look for the built-in constant __OCTAVE_VERSION__,
which seems unlikely to be set in a user's Matlab startup file.
I think something like

  is_octave = exist ('__OCTAVE_VERSION__') ~= 0;

should do the trick.

If you think someone might actually define a variable with this name
in their Matlab startup file, you could try an operation that will
only work in Octave.  Perhaps omething like
 
  is_octave = eval ('isstr ("Matlab chokes on double quotes");', '0');

would work (until Matlab supports double quotes, anyway).  Maybe it's
best to put the test in an M-file so that you can easily change it for
all your scripts if needed.

jwe


Reply | Threaded
Open this post in threaded view
|

How can a script tell if it's running on Octave or Matlab?

John W. Eaton-6
In reply to this post by ldoolitt-2
On 25-Sep-1998, [hidden email] <[hidden email]> wrote:

| I found
| a difference in the default behavior of the str2num function.  Will
| this be stable?
|
| I ran the following on Matlab and  Octave:
|  v=version
|  vn=str2num(version)
|
| Matlab (extraneous vertical space deleted):
| v = 5.1.0.421
| vn = 5.1000         0    0.4210
|
| Octave (somewhat less extraneous vertical space deleted):
| v = 2.0.13
| vn = [](0x0)

Octave's str2num function works more like the one from Matlab 4.x, not
5.x.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: How can a script tell if it's running on Octave or Matlab?

Frederic Gobry
In reply to this post by ldoolitt-2
Hi,

This doesn't solve your problem directly, but for specific cases (like using
varargs in functions) you can look at the attached script. It uses m4 (in
its current incarnation, one has to use GNU m4) to help generating two
versions out of one main file. For example, you can write in a .m file :

%in_matlab(
... some matlab specific stuff ...
%)

%in_octave(
... octave code ....
%)

and then use

        m-specific octave source_file.m target.m
or m-specific matlab source_file.m target.m
       
to generate a file containing the parts that correspond to a given platform.

Hope this helps,
Frédéric

--
    Frédéric GOBRY                  IDIAP
                               Rue du Simplon 4,
  Research Assistant          CH 1920 - Martigny
Machine Learning Group      Tel: +41 27 721 77 31

m-specific (2K) Download Attachment