Run octave script file containing a function definition
I've a very newbie octave question.
Running this code in octave console is working fine:
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
By inserting this code in an external file named for example file.m, and executing it through octave file.m
an error occurs:
warning: function name 'recfibo' does not agree with function filename
'/Users/admin/Google Drive/file.m'
error: 'n' undefined near line 2 column 8 error: called from
octave at line 2 column 3
How should I resolve this particular problem?
octave
add a comment |
I've a very newbie octave question.
Running this code in octave console is working fine:
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
By inserting this code in an external file named for example file.m, and executing it through octave file.m
an error occurs:
warning: function name 'recfibo' does not agree with function filename
'/Users/admin/Google Drive/file.m'
error: 'n' undefined near line 2 column 8 error: called from
octave at line 2 column 3
How should I resolve this particular problem?
octave
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29
add a comment |
I've a very newbie octave question.
Running this code in octave console is working fine:
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
By inserting this code in an external file named for example file.m, and executing it through octave file.m
an error occurs:
warning: function name 'recfibo' does not agree with function filename
'/Users/admin/Google Drive/file.m'
error: 'n' undefined near line 2 column 8 error: called from
octave at line 2 column 3
How should I resolve this particular problem?
octave
I've a very newbie octave question.
Running this code in octave console is working fine:
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
By inserting this code in an external file named for example file.m, and executing it through octave file.m
an error occurs:
warning: function name 'recfibo' does not agree with function filename
'/Users/admin/Google Drive/file.m'
error: 'n' undefined near line 2 column 8 error: called from
octave at line 2 column 3
How should I resolve this particular problem?
octave
octave
asked Jan 2 at 21:13
DavideDavide
485517
485517
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29
add a comment |
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29
add a comment |
2 Answers
2
active
oldest
votes
As stated in the answer provided by @CrisLuengo here you have created a function file instead of a script file and they are treated differently
in Octave. Because it is a function file Octave executes it by calling the function it defines with no arguments and nargout = 0
. So you will get an error that n
is undefined.
Another problem is that the function name 'recfibo'
does not agree with function filename 'file'
. In such cases Octave internally changes the name of the function to the name of the function file so the name is changed to 'file'
. Therefor Octave and the function itself will forget the original function name and unfortunately the function cannot call itself recursively!
I like the @CrisLuengo 's answer but I think the more idiomatic and preferable way is always using function files instead of script files, though the script file solution is the only solution that works in previous Octave versions (Octave 3.X).
You can change your code to:
function file
disp(recfibo(5))
endfunction
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
add a comment |
Add 1;
as the first line of the file:
1;
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
Any M-file that starts with a function definition is a function M-file, not a script M-file. By adding a meaningless statement to the top, you turn it into a script.
In MATLAB (since fairly recently), a script M-file can define functions at the end of the script. There you'd put the disp
line at the top of the file, and have the function
block at the end, without any script lines after it. However, Octave requires functions to be defined before you use them, hence it has to come before the script line that uses the function. Octave allowed the definition of functions within a script file before MATLAB introduced that feature, hence their implementation is not compatible with that of MATLAB.
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013267%2frun-octave-script-file-containing-a-function-definition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As stated in the answer provided by @CrisLuengo here you have created a function file instead of a script file and they are treated differently
in Octave. Because it is a function file Octave executes it by calling the function it defines with no arguments and nargout = 0
. So you will get an error that n
is undefined.
Another problem is that the function name 'recfibo'
does not agree with function filename 'file'
. In such cases Octave internally changes the name of the function to the name of the function file so the name is changed to 'file'
. Therefor Octave and the function itself will forget the original function name and unfortunately the function cannot call itself recursively!
I like the @CrisLuengo 's answer but I think the more idiomatic and preferable way is always using function files instead of script files, though the script file solution is the only solution that works in previous Octave versions (Octave 3.X).
You can change your code to:
function file
disp(recfibo(5))
endfunction
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
add a comment |
As stated in the answer provided by @CrisLuengo here you have created a function file instead of a script file and they are treated differently
in Octave. Because it is a function file Octave executes it by calling the function it defines with no arguments and nargout = 0
. So you will get an error that n
is undefined.
Another problem is that the function name 'recfibo'
does not agree with function filename 'file'
. In such cases Octave internally changes the name of the function to the name of the function file so the name is changed to 'file'
. Therefor Octave and the function itself will forget the original function name and unfortunately the function cannot call itself recursively!
I like the @CrisLuengo 's answer but I think the more idiomatic and preferable way is always using function files instead of script files, though the script file solution is the only solution that works in previous Octave versions (Octave 3.X).
You can change your code to:
function file
disp(recfibo(5))
endfunction
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
add a comment |
As stated in the answer provided by @CrisLuengo here you have created a function file instead of a script file and they are treated differently
in Octave. Because it is a function file Octave executes it by calling the function it defines with no arguments and nargout = 0
. So you will get an error that n
is undefined.
Another problem is that the function name 'recfibo'
does not agree with function filename 'file'
. In such cases Octave internally changes the name of the function to the name of the function file so the name is changed to 'file'
. Therefor Octave and the function itself will forget the original function name and unfortunately the function cannot call itself recursively!
I like the @CrisLuengo 's answer but I think the more idiomatic and preferable way is always using function files instead of script files, though the script file solution is the only solution that works in previous Octave versions (Octave 3.X).
You can change your code to:
function file
disp(recfibo(5))
endfunction
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
As stated in the answer provided by @CrisLuengo here you have created a function file instead of a script file and they are treated differently
in Octave. Because it is a function file Octave executes it by calling the function it defines with no arguments and nargout = 0
. So you will get an error that n
is undefined.
Another problem is that the function name 'recfibo'
does not agree with function filename 'file'
. In such cases Octave internally changes the name of the function to the name of the function file so the name is changed to 'file'
. Therefor Octave and the function itself will forget the original function name and unfortunately the function cannot call itself recursively!
I like the @CrisLuengo 's answer but I think the more idiomatic and preferable way is always using function files instead of script files, though the script file solution is the only solution that works in previous Octave versions (Octave 3.X).
You can change your code to:
function file
disp(recfibo(5))
endfunction
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
edited Jan 3 at 5:27
answered Jan 3 at 4:58


rahnema1rahnema1
10.7k2923
10.7k2923
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
add a comment |
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
1
1
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
I agree it is better to always write functions. It keeps the base workspace clean.
– Cris Luengo
Jan 3 at 5:06
add a comment |
Add 1;
as the first line of the file:
1;
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
Any M-file that starts with a function definition is a function M-file, not a script M-file. By adding a meaningless statement to the top, you turn it into a script.
In MATLAB (since fairly recently), a script M-file can define functions at the end of the script. There you'd put the disp
line at the top of the file, and have the function
block at the end, without any script lines after it. However, Octave requires functions to be defined before you use them, hence it has to come before the script line that uses the function. Octave allowed the definition of functions within a script file before MATLAB introduced that feature, hence their implementation is not compatible with that of MATLAB.
add a comment |
Add 1;
as the first line of the file:
1;
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
Any M-file that starts with a function definition is a function M-file, not a script M-file. By adding a meaningless statement to the top, you turn it into a script.
In MATLAB (since fairly recently), a script M-file can define functions at the end of the script. There you'd put the disp
line at the top of the file, and have the function
block at the end, without any script lines after it. However, Octave requires functions to be defined before you use them, hence it has to come before the script line that uses the function. Octave allowed the definition of functions within a script file before MATLAB introduced that feature, hence their implementation is not compatible with that of MATLAB.
add a comment |
Add 1;
as the first line of the file:
1;
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
Any M-file that starts with a function definition is a function M-file, not a script M-file. By adding a meaningless statement to the top, you turn it into a script.
In MATLAB (since fairly recently), a script M-file can define functions at the end of the script. There you'd put the disp
line at the top of the file, and have the function
block at the end, without any script lines after it. However, Octave requires functions to be defined before you use them, hence it has to come before the script line that uses the function. Octave allowed the definition of functions within a script file before MATLAB introduced that feature, hence their implementation is not compatible with that of MATLAB.
Add 1;
as the first line of the file:
1;
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction
disp(recfibo(5))
Any M-file that starts with a function definition is a function M-file, not a script M-file. By adding a meaningless statement to the top, you turn it into a script.
In MATLAB (since fairly recently), a script M-file can define functions at the end of the script. There you'd put the disp
line at the top of the file, and have the function
block at the end, without any script lines after it. However, Octave requires functions to be defined before you use them, hence it has to come before the script line that uses the function. Octave allowed the definition of functions within a script file before MATLAB introduced that feature, hence their implementation is not compatible with that of MATLAB.
edited Jan 2 at 21:33
answered Jan 2 at 21:29


Cris LuengoCris Luengo
22.4k52253
22.4k52253
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013267%2frun-octave-script-file-containing-a-function-definition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hummm, I was sure there was a duplicate but cannot find it (except for one without answers). I've turned my comment into an answer.
– Cris Luengo
Jan 2 at 21:29