Check if the environment is Octave or Matlab

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Check if the environment is Octave or Matlab

José Luis García Pallero
Hello,

Sometimes I need to check if a function or script is running in Octave
or Matlab. Usually I make the check as

if exist('OCTAVE_VERSION')
    octave = 1;
else
    octave = 0;
end

Is this the FASTEST way to check the environment or is there any other
method more suitable?

Thanks
--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

PhilipNienhuis
José Luis García Pallero wrote

> Hello,
>
> Sometimes I need to check if a function or script is running in Octave
> or Matlab. Usually I make the check as
>
> if exist('OCTAVE_VERSION')
>     octave = 1;
> else
>     octave = 0;
> end
>
> Is this the FASTEST way to check the environment or is there any other
> method more suitable?

We have more or less the same in a function "ïsOctave.m" (maybe even with a
persistent variable inside). That function lives in our script library (a
collection of dedicated m-file subdirs) that is in a path set up in a
startup script called by both Matlab and Octave.
No idea if it's faster (I do suspect some function calling overhead) but as
a function it's easy to put in an if clause:
if (isOctave)
  ...
else
  ...
end %if

Philip




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

PhilipNienhuis
PhilipNienhuis wrote

> José Luis García Pallero wrote
>> Hello,
>>
>> Sometimes I need to check if a function or script is running in Octave
>> or Matlab. Usually I make the check as
>>
>> if exist('OCTAVE_VERSION')
>>     octave = 1;
>> else
>>     octave = 0;
>> end
>>
>> Is this the FASTEST way to check the environment or is there any other
>> method more suitable?
>
> We have more or less the same in a function "ïsOctave.m" (maybe even with
> a
> persistent variable inside). That function lives in our script library (a
> collection of dedicated m-file subdirs) that is in a path set up in a
> startup script called by both Matlab and Octave.
> No idea if it's faster (I do suspect some function calling overhead) but
> as
> a function it's easy to put in an if clause:
> if (isOctave)
>   ...
> else
>   ...
> end %if
>
> Philip

Now I remember that someone suggested us to declare a global variable
isOctave=0 in Matlab's startup.m file (to be put in <MATLAB>\bin\ next to
matab.exe), and similarly, global isOctave=1 in the site-wide .octaverc file
(in share/octave/<version>/m/startup/
That should have the lowest overhead.
But in controlled (locked down) environments it may require a bit of effort
to get IT people to do this for you.

P.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

steph
In reply to this post by PhilipNienhuis
On 2020/04/10 16:17, PhilipNienhuis wrote:
José Luis García Pallero wrote
Hello,

Sometimes I need to check if a function or script is running in Octave
or Matlab. Usually I make the check as

if exist('OCTAVE_VERSION')
    octave = 1;
else
    octave = 0;
end

Is this the FASTEST way to check the environment or is there any other
method more suitable?
We have more or less the same in a function "ïsOctave.m" (maybe even with a
persistent variable inside). That function lives in our script library (a
collection of dedicated m-file subdirs) that is in a path set up in a
startup script called by both Matlab and Octave.
No idea if it's faster (I do suspect some function calling overhead) but as
a function it's easy to put in an if clause:
if (isOctave)
  ...
else
  ...
end %if

Philip




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



I tried isOctave with following results

>> isOctave
error: 'isOctave' undefined near line 1 column 1
>>

Is there a package that I should load? I run Octave 5.2 on Ubuntu 18.04.3

--
Steph


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

James Sherman

On Fri, Apr 10, 2020 at 2:50 PM Steph Bredenhann <[hidden email]> wrote:
On 2020/04/10 16:17, PhilipNienhuis wrote:
José Luis García Pallero wrote
Hello,

Sometimes I need to check if a function or script is running in Octave
or Matlab. Usually I make the check as

if exist('OCTAVE_VERSION')
    octave = 1;
else
    octave = 0;
end

Is this the FASTEST way to check the environment or is there any other
method more suitable?
We have more or less the same in a function "ïsOctave.m" (maybe even with a
persistent variable inside). That function lives in our script library (a
collection of dedicated m-file subdirs) that is in a path set up in a
startup script called by both Matlab and Octave.
No idea if it's faster (I do suspect some function calling overhead) but as
a function it's easy to put in an if clause:
if (isOctave)
  ...
else
  ...
end %if

Philip




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



I tried isOctave with following results

>> isOctave
error: 'isOctave' undefined near line 1 column 1
>>

Is there a package that I should load? I run Octave 5.2 on Ubuntu 18.04.3

--
Steph

Hi Steph,

I think you misread that response.  I believe what Philip was saying that he put that code (starting with if exist(...)) in a file called isOctave.  I don't believe there is a built in function to do what you're asking.  The caveat was that, if you're looking for the FASTEST way, it would evoke some function call overhead than putting that code directly into your code, so it would be relatively slower than putting it inline.

James Sherman


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

steph

Oh, my apologies!


On 2020/04/10 20:56, James Sherman Jr. wrote:

On Fri, Apr 10, 2020 at 2:50 PM Steph Bredenhann <[hidden email]> wrote:
On 2020/04/10 16:17, PhilipNienhuis wrote:
José Luis García Pallero wrote
Hello,

Sometimes I need to check if a function or script is running in Octave
or Matlab. Usually I make the check as

if exist('OCTAVE_VERSION')
    octave = 1;
else
    octave = 0;
end

Is this the FASTEST way to check the environment or is there any other
method more suitable?
We have more or less the same in a function "ïsOctave.m" (maybe even with a
persistent variable inside). That function lives in our script library (a
collection of dedicated m-file subdirs) that is in a path set up in a
startup script called by both Matlab and Octave.
No idea if it's faster (I do suspect some function calling overhead) but as
a function it's easy to put in an if clause:
if (isOctave)
  ...
else
  ...
end %if

Philip




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



I tried isOctave with following results

>> isOctave
error: 'isOctave' undefined near line 1 column 1
>>

Is there a package that I should load? I run Octave 5.2 on Ubuntu 18.04.3

--
Steph

Hi Steph,

I think you misread that response.  I believe what Philip was saying that he put that code (starting with if exist(...)) in a file called isOctave.  I don't believe there is a built in function to do what you're asking.  The caveat was that, if you're looking for the FASTEST way, it would evoke some function call overhead than putting that code directly into your code, so it would be relatively slower than putting it inline.

James Sherman
--
Steph Bredenhann PrEng, PrCPM


Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

Mike Miller-4
In reply to this post by José Luis García Pallero
On Fri, Apr 10, 2020 at 14:32:21 +0200, José Luis García Pallero wrote:

> Hello,
>
> Sometimes I need to check if a function or script is running in Octave
> or Matlab. Usually I make the check as
>
> if exist('OCTAVE_VERSION')
>     octave = 1;
> else
>     octave = 0;
> end
>
> Is this the FASTEST way to check the environment or is there any other
> method more suitable?
The user manual has a suggestion that follows the same lines:

    https://octave.org/doc/interpreter/How-to-Distinguish-Between-Octave-and-Matlab.html

As the manual shows, if you are assigning a variable anyway, avoid the
if-else:

    octave = (exist('OCTAVE_VERSION', 'builtin') == 5);

and as others have suggested, you could make that persistent and/or
global, depending on your needs.

Cheers,

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

nrjank
Administrator
In reply to this post by steph
> I think you misread that response.  I believe what Philip was saying that he put that code (starting with if exist(...)) in a file called isOctave.  I don't believe there is a built in function to do what you're asking.  The caveat was that, if you're looking for the FASTEST way, it would evoke some function call overhead than putting that code directly into your code, so it would be relatively slower than putting it inline.

is this something you're looking to do repeatedly?   if so the Octave manual and wiki offer the following suggestion to use a persistent variable with is_octave so you only have to call exist once:

-----------------
%%
%% Return: true if the environment is Octave.
%%
function retval = isOctave
  persistent cacheval;  % speeds up repeated calls

  if isempty (cacheval)
    cacheval = (exist ("OCTAVE_VERSION", "builtin") > 0);
  end

  retval = cacheval;
end
-------------------



Reply | Threaded
Open this post in threaded view
|

Re: Check if the environment is Octave or Matlab

Colin Macdonald-2
In reply to this post by Mike Miller-4
On 2020-04-10 12:21 p.m., Mike Miller wrote:
> As the manual shows, if you are assigning a variable anyway, avoid the
> if-else:
>
>      octave = (exist('OCTAVE_VERSION', 'builtin') == 5);
>
> and as others have suggested, you could make that persistent and/or
> global, depending on your needs.

We did some tests in the doctest project and decided that caching in a
persistent variable wasn't worth it.  Here's our function, which has
some timing info in the docstring:


function r = is_octave()
%IS_OCTAVE  True if we are running Octave, false for Matlab.

%%
% Copyright (c) 2015 Colin B. Macdonald
% SPDX-License-Identifier: BSD-3-Clause


% Timings for different implementations, 10000 calls
%
%     test        Matlab    Octave
%     ----------------------------
%     try-catch   1.2s      0.18s
%     if-exist    0.14s     0.22s
%     dummy       0.13s     0.13s
%
% Conclusions: "if-exist" only twice as slow as "dummy" (always return
% true), so no need to bother with a persistent variable.

   r = exist('OCTAVE_VERSION', 'builtin') ~= 0;

   %try
   %  OCTAVE_VERSION;
   %  r = true;
   %catch
   %  r = false;
   %end

end