Clear Filters
Clear Filters

Unable to recognise function from called script

36 views (last 30 days)
Alexandra
Alexandra on 23 Aug 2024 at 10:10
Answered: Steven Lord on 23 Aug 2024 at 12:51
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 Comments
Stephen23
Stephen23 on 23 Aug 2024 at 10:18
Edited: Stephen23 on 23 Aug 2024 at 11:23
"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.
Alexandra
Alexandra on 23 Aug 2024 at 10:59
Ohh ok, I understand the error I have made now. Thank you so much for your help!

Sign in to comment.

Accepted Answer

Torsten
Torsten on 23 Aug 2024 at 10:18
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

More Answers (2)

arushi
arushi on 23 Aug 2024 at 10:18
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
Steven Lord on 23 Aug 2024 at 12:51
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!