我需要编写能够在Octave和MATLAB中运行的代码。问题是它需要处理一些GUI相关的内容,而MATLAB和Octave的处理方式完全不同。
有没有办法检测我是否在运行MATLAB或Octave,以便调用正确的函数?
我需要编写能够在Octave和MATLAB中运行的代码。问题是它需要处理一些GUI相关的内容,而MATLAB和Octave的处理方式完全不同。
有没有办法检测我是否在运行MATLAB或Octave,以便调用正确的函数?
你可以使用以下测试来区分 Octave 和 MATLAB:
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
function foo
%% fancy code that works in both
if (is_octave)
%% use octave super_powers
else
%% do it matlab way
end
%% fancy code that works in both
end
%% subfunction that checks if we are in octave
function r = is_octave ()
persistent x;
if (isempty (x))
x = exist ('OCTAVE_VERSION', 'builtin');
end
r = x;
end
MATLAB Version 7.7.0.471 (R2008b) Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686 Java VM Version: Java 1.6.0_04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
GNU Octave Version 3.0.5 GNU Octave License: GNU General Public License Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686
在Matlab中:
>> exist __octave_config_info__
ans =
0
在Octave中:
octave:3> exist __octave_config_info__
ans = 5
ans = 0
的结果! - winkmalexist __octave_config_info__
而不是 octave_config_info
,因为 octave_config_info
已过时。更多信息请查看此处。 - Foad S. Farimani