Octave Function Undefined - function

Any idea why the following produces an error?
printf("this is my text\n");
MyFunc;
function MyFunc
printf("printing from inside function\n");
endfunction
This is the error I get (from the Command Window)
error: 'MyFunc' undefined near line 3, column 3
error: called from
function_example at line 3 column 1

Okay, I see the issue now. The function definition needs to be placed before the function call is made - as shown below.
printf("this is my text\n");
function MyFunc
printf("printing from inside function\n");
endfunction
MyFunc;

Related

Octave doesn't disp my values, and only stores in ans variable [duplicate]

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?
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.
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

Do functions always require their own separate files? [duplicate]

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?
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.
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

Why Octave doesn't see this object?

I wanted to code a function in octave, however I found some problems during doing so:
N=700;
T=900;
lambda=N/T;
x=randn(N,T);
s=std(x(:));
r=x*x'/T;
l=eig(r);
lambda_plus=(s^2)*(1+sqrt(lambda))^2;
lambda_minus=(s^2)*(1-sqrt(lambda))^2;
# Define a function - value of this function depenends
# on position of x
function kiki = avg (x)
if (x <= lambda_plus && x >= lambda_minus)
(1/(2*pi*lambda*x*s^(2)))*sqrt((lambda_plus-x)*(x-lambda_minus));
else
0
endif
endfunction
Then I wanted to run this function so I did run avg(2) but it didn't work.
Error I saw was:
error: 'lambda_plus' undefined near line 15, column 15
but it's not true! lambda_plus is defined before the definition of the function!
I read that this problem might be, because octave doesn't see our function and the solution is to:
(1) Save the file with name of the function - in my case avg.m
(2) Open a new file and in new file run your function
I did exactly what they were saying and in newly created file I ran avg(2) but unfortunately with exactly same result.

Error : 'x' undefined

I got a problem with running Octave function (ODE), I've tried already present solutions for this problem but nothing is working. I've also tried by saving my filename as egzamin.m but it too not worked.
Code from octave :
function dx=egzamin(x,t)
dx=zeros(4,1);
b=0;
g=9.81;
x1=x(1);
y1=x(2);
Vx=x(3);
Vy=x(4);
dx(1)=Vx;
dx(2)=Vy;
dx(3)=-b*Vx*sqrt(Vx.^2+Vy.^2);
dx(4)=-b*Vy*sqrt(Vx.^2+Vy.^2)-g;
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
The error is :
error: 'x' undefined near line 5 column 4
error: called from
egzamin at line 5 column 3
Since the file starts with function, it is not a script file,
as explained in the doc:
Unlike a function file, a script file must not begin with the keyword
function
Add any statement (even dummy like 1;) before the function line to get a script file.
# dummy statement to get a script file instead of a function file
1;
function dx=egzamin(x,t)
g = 9.81;
Vx = x(3);
Vy = x(4);
dx = [Vx, Vy, 0, -g];
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
A very clear explanation of what's going on is given here.
You need to save the function (thus from function to endfunction and naught else) as egzamin.m, and then execute the rest of the code in a script or at the command line. Alternatively, provided Octave does that the same as what MATLAB does nowadays, first put your script (N=(..) to plot()) and then the function.
This is necessary since you are defining your function first, so it doesn't have any inputs yet, as you don't define them until later. The function needs to have its inputs defined before it executes, hence you need to save your function separately.
You can of course save your "script" bit, thus everything which is currently below your function declaration, as a function as well, simply don't give it in- and outputs, or, set all the input parameters here as well. (Which I wouldn't do as it's the same as your
egzamin then.) e.g.
function []=MyFunc()
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
endfunction

Scilab Error 4 Undefined variable <function_name>

hi ppl im new at Scilab,
im doing a script which i send the user through menus and i'm making a function for every sub-menu.
I read that functions can have 0 input parameters but it must have at least 1 output parameter.
According to this i wrote this
//execution file landing menu
option='1'
while (option~=0)
clc
mprintf('1 - First Sub-menu')
mprintf('2 - Second Sub-menu')
option=input('Select the option: ', 's')
select option
case '1' then
result=sub_one(),
case '2' then
result=sub_two(),
else
disp('Invalid Option!!!')
end
end
//Function sub_one
function result=sub_one()
option='1'
while (option~=0)
clc
mprintf('1 - Do stuff')
mprintf('2 - Do other stuff')
option=input('Select the option: ', 's')
select option
case '1' then
result=do_stuff(),
case '2' then
result=do_other_stuff(),
else
disp('Invalid Option!!!')
end
end
result=0
endfunction
and i always get the error
result=sub_one(),
!--error 4
Undefined variable: sub_one
at line xx of exec file called by :
exec('dir\dir\dir\file.sce', -1)
this is freaking annoying me
some experter then me?
Scilab parses the file top to bottom, so when it is in your main part at the top of the file, sub_one does not exist yet. If you switch the order around it will work.
If you would like to retain the order in your file you can also do the following:
// Define some main function
function main()
disp('hello from main')
sub_one()
endfunction
// Define some sub function
function sub_one()
disp('hello from sub_one')
endfunction
// call function main to execute
main()