4

我有一些代码需要在 MATLAB 和 Freemat 中运行,在每种情况下使用不同的服务函数。为了使整个事情可移植,我需要找到一种方法来确定在代码开始时要调用哪些函数,具体取决于我所处的环境。

如何做到这一点?

我正在考虑使用version命令,但不太确定这是否真的很强大。

4

2 回答 2

3

在\verstring的上下文中使用,matlab 中的输出将是一个错误,而在 Freemat 中它将类似于“freemat 4.0”,例如:trycatch

try
    txt=verstring;
    output='Freemat';
catch err
    output='Matlab';
end
于 2013-11-19T21:35:15.317 回答
2

我会尝试识别每一个,如果不确定会给出错误,或者在这种情况下要求用户手动识别它:

% try to identify if it is freeMat
isDefinatelyFreeMat = false;
try
    versionIdentifier=verstring;
    if (strcmpi(versionIdentifier(1:7), 'FreeMat'))
        isDefinatelyFreeMat = true;
    end
catch e
end

% try to identify if it is Matlab
isDefinatelyMatlab = false;
try
    versionIdentifier=ver;
    if (strcmpi(versionIdentifier.Name, 'Matlab'))
        isDefinatelyMatlab = true;
    end
catch e
end

% if identification was not successful
if ((isDefinatelyFreeMat && isDefinatelyMatlab) || (~isDefinatelyFreeMat && ~isDefinatelyMatlab))
    error('Was unable to identify software.');
    % TODO: Ask user to identify software manually
end
于 2013-11-20T08:00:09.380 回答