Are there C like pre-processor directives for Octave and Scilab to be used for intercompatible code?

谁都会走 提交于 2019-12-02 04:47:54

I want to answer from a different angle. Namely, if you feel the need to compare preprocessor directives, you may be thinking about scilab and octave all wrong. The reason preprocessor directives are necessary in C and C++ is because those are compiled languages. The preprocessor directives make changes to the actual code that will be compiled, before compilation takes place.

In an interpreted language like matlab, scilab and octave, this kind of thing is redundant. So a simple 'if / else' block performing a test that adequately distinguishes between the three environments should be adequate.

The octave manual suggests a way to distinguish between octave and matlab that does not carry a heavy performance penalty. I don't have scilab installed to provide an equivalent test, but I'm sure a simple test exists for scilab as well.

So, in the context of running different code by detecting the right environment, this is totally possible.

In the context of imitating an #include strategy, since a script is run sequentially, you could implement an 'if / else' block which simply runs a different base script at the right time.

PS. Matlab has been making some changes in the way scripts are interpreted, so this may lead to problems if this performs 'nested' error-checking rather than superficial error-checking. But, even if this does happen, simply instead of calling a script directly, you can use the run filename syntax instead, or, worse case scenario, use eval to call the script.

rahnema1

You can define a function isscilab:

function [out] = isscilab()
        out = length(zeros(2)) == 1;
endfunction

And use it to conditionally execute the code:

if isscilab()
    do scilab...
else
    do octave...
end

But I think the best choice is that you should implement different files for Octave .m and Scilab .sce and execute each one you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!