use*_*ser 4 matlab function octave
如果我这样写:
clc
clear
close all
format long
fprintf( 1, 'Starting...\n' )
function results = do_thing()
results = 1;
end
results = do_thing()
Run Code Online (Sandbox Code Playgroud)
并使用 运行它Octave,它可以正常工作:
Starting...
results = 1
Run Code Online (Sandbox Code Playgroud)
但如果我尝试使用 运行它Matlab 2017b,它会抛出此错误:
Error: File: testfile.m Line: 13 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "do_thing" function definition to before the first local function
definition.
Run Code Online (Sandbox Code Playgroud)
然后,如果我按如下方式修复错误:
Starting...
results = 1
Run Code Online (Sandbox Code Playgroud)
它可以正确运行在Matlab:
Starting...
results =
1
Run Code Online (Sandbox Code Playgroud)
但现在,它停止使用Octave:
Starting...
error: 'do_thing' undefined near line 8 column 11
error: called from
testfile at line 8 column 9
Run Code Online (Sandbox Code Playgroud)
这个问题在这个问题上得到了解释:运行包含函数定义的八度脚本文件
如何修复它,而无需为该函数创建单独的独占文件do_thing()?
这个问题在某些较新版本的Matlabas上得到解决吗2019a?
答案在评论中,但为了清楚起见:
% in file `do_thing.m`
function results = do_thing()
results = 1;
end
% in your script file
clc; clear; close all; format long;
fprintf( 1, 'Starting...\n' );
results = do_thing();
Run Code Online (Sandbox Code Playgroud)
随附的解释性咆哮: