Defining a function in one file and using it in a second file
58 views (last 30 days)
Show older comments
Vicki
on 2 Dec 2025 at 9:56
Commented: Vicki
on 3 Dec 2025 at 9:17
I have one code file that defines two functions. The second function operates on the output of the first function:
function SoilResp_gC = SoilResp(Temp,Precip)
SoilResp_gC = F.*(exp(Q*Temp)).*(Precip./(K+Precip));
end
function SoilResp_gO2 = UnitConvert(SoilResp_gC)
SoilResp_molesC = SoilResp_gC./12.01;
SoilResp_molesO2 = SoilResp_molesC;
SoilResp_gO2 = SoilResp_molesO2.*32;
end
I have a second file which defines some constants (including those required by these functions, Temp and Precip) and then runs for loop to iteratively add data to a matrix. The first steps in this loop are the functions I defined in the first file:
for i = 2:NumTimesteps % for each timestep
% 1. Soil Respiration % as per Reich, 2002
SoilResp_gC(Temp,Precip)
SoilResp_gO2(SoilResp_gC)
...
I first run the file with the functions in. I get no errors. I then run the file with the loop, and get the following error message:
Error in Filename (line 38) SoilResp_gC(Temp,Precip)
Undefined function 'SoilResp_gC' for input arguments of type 'double'.
What do I need to do to make sure my function is recognised and properly defined, so that the second file will run correctly?
0 Comments
Accepted Answer
Star Strider
on 2 Dec 2025 at 11:36
I would save each of your functions to a separate .m file. That way, they should always have access to each other.
NOTE: The functions are actually named 'SoilResp' and ''UnitConvert'.You have to call them by their assigned names.
2 Comments
Star Strider
on 2 Dec 2025 at 12:08
Edited: Star Strider
on 2 Dec 2025 at 15:49
I'm not certain what you're doing.
If you want 'SoilResp_gC' you have to call 'SoilResp' with the 'Temp' and 'Precip' arguments and return the output to a variable.
Perhaps:
for i = 2:NumTimesteps % for each timestep
SoilResp_gC(i) = SoilResp(Temp,Precip);
SoilResp_gO2(i) = UnitConvert(SoilResp_gC(i));
...
end
or something similar.
.
EDIT -- (2 Dec 2025 at 12:36)
The function name and the .m file name should be the same. (Missed that befOre. Early here.)
.
EDIT -- (2 Dec 2025 at 15:20)
The script .m file name cannot be the same as any local function defined within it. That confuses MATLAB.
The function .m file name is usually the name of the function defined within it. There can be exceptions to that, however making the function .m file name the same as the function name is preferred.
.
EDIT -- (2 Dec 2025 at 15:49)
'How do I convert the file to a function file from a script file?'
The easiest way is probably to rename the script file and save it with the new name, then copy the function to a new Editor tab, then save it as the function name.
.
More Answers (2)
Torsten
on 2 Dec 2025 at 12:12
Edited: Torsten
on 2 Dec 2025 at 12:15
The calls must be
for i = 2:NumTimesteps % for each timestep
% 1. Soil Respiration % as per Reich, 2002
SoilResp_gC = SoilResp(Temp,Precip)
SoilResp_gO2 = UnitConvert(SoilResp_gC)
...
not
for i = 2:NumTimesteps % for each timestep
% 1. Soil Respiration % as per Reich, 2002
SoilResp_gC(Temp,Precip)
SoilResp_gO2(SoilResp_gC)
...
And the functions should be named
SoilResp.m
UnitConvert.m
not
SoilResp_Func.m
UnitConvert_Func.m
4 Comments
Steven Lord
on 2 Dec 2025 at 15:27
Script files
Inside of a script file, all functions defined inside it are local functions. That means they can only be called directly from within that script file. [There is a way to indirectly call them, if the script file creates a function handle variable that other code has access to.]
Local functions inside a script file cannot have the same name as the script file itself. [If I had a script foo.m with a local function foo inside it, and I called foo inside that script file, would that be an attempt to call the local function with 0 input arguments or an attempt to rerun the script file? It's ambiguous, so we disallow it.]
Function files
Inside of a function file, only the first function in the file (the main function) is directly callable from outside the function file. All others are local functions and can only be called directly from within that function file. [The same comment about function handles applies here, though in this case the main function would need to return that variable or find some other way to make it available to other code in order for that other code to use it.] From the "Contents of Functions and Files" section on that documentation page to which I linked:
"Program files can contain multiple functions. If the file contains only function definitions, the first function is the main function, and is the function that MATLAB associates with the file name. Functions that follow the main function or are included in script code are called local functions. Local functions are only available within the file."
A note on names: if the name of the main function in the function file and the name of the function file itself differ, you will need to use the file name to call the main function. So if I had a file timestwo.m and its main function was named timesthree, I would need to call the function as timestwo (and should change one of the names to match the desired functionality.)
Class files
This gets a little tricky. Class files can have methods that may or may not be accessible to outside files (depending on how they are defined, in particular the Access attribute in their methods block) or local functions (which are not accessible outside the class.) I'm not going to go into detail here to try to avoid confusing you. [Probably too late.]
3 Comments
Stephen23
on 2 Dec 2025 at 16:16
The difference between a script and a function is simple: anything that is outside of FUNCTION ... END makes it a script. So to make an Mfile a function you must make sure that it has absolutely no code outside of FUNCTION ... END, for example by deleting all code outside of FUNCTION ... END (or moving it to another script or function) or by placing FUNCTION ... END around all code in an Mfile.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!