Unable to recognise function from called script
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alexandra
el 23 de Ag. de 2024
Respondida: Steven Lord
el 23 de Ag. de 2024
I created this function in a scipted named "lab1_script.m":
t = [1, 2, 3, 4]
function y = lab1_function(t, c)
y = c*t.^2;
end
But when I try to call the script in a new script as follows:
lab1_script;
y = lab1_function(t,0.1)
The error message: unrecognisable function or variable 'lab1_function' appears. And I know that the function works, because when I call it within the same script it is fine. It is just trying to call it into a different script that I am having trouble with.
I am confused, what am I doing wrong? Should I be calling the script a different way for the function to be recognised?
Thanks in advance! :)
2 comentarios
Stephen23
el 23 de Ag. de 2024
Editada: Stephen23
el 23 de Ag. de 2024
"I created this function in a scipted named "lab1_script.m""
t = [1, 2, 3, 4] % <---------------- !!!!! DELETE THIS LINE !!!!
function y = lab1_function(t, c)
y = c*t.^2;
end
When you added that line of code you turned the Mfile into a script with a local function:
Local functions are only callable from within the same Mfile.
Respuesta aceptada
Torsten
el 23 de Ag. de 2024
The script should be
t = [1, 2, 3, 4];
y = lab1_function(t,0.1)
the function should be
function y = lab1_function(t, c)
y = c*t.^2;
end
Or alternatively you can use two nested functions:
lab1_script
function lab1_script
t = [1, 2, 3, 4];
y = lab1_function(t,0.1)
function y = lab1_function(t, c)
y = c*t.^2;
end
end
Más respuestas (2)
arushi
el 23 de Ag. de 2024
Hi Alexandra,
"t = [1, 2, 3, 4]" should be defined inside the function.
In MATLAB, functions defined within a script are local to that script and cannot be accessed from other scripts or the command line. To resolve this, you should define your function in a separate file or convert your script into a function file.
Hope this helps.
Steven Lord
el 23 de Ag. de 2024
Local functions in a script are only directly callable from within that script file. You could call them indirectly by calling the script and having it create a function handle to the local function. As long as the function handle was created inside the script, even if it's used outside the script it will be able to call the local function.
For example I created this script:
fh = @mylocalfunction;
fh("inside script");
function mylocalfunction(whereCalled)
fprintf("The local function mylocalfunction was called %s", whereCalled)
end
When I ran it in my MATLAB Online session the output was:
>> example2147479
The local function mylocalfunction was called inside script
Then I typed one more line in the Command Window and the output was:
>> fh("outside script")
The local function mylocalfunction was called outside script
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!