Defining a function in one file and using it in a second file
Show older comments
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?
Accepted Answer
More Answers (2)
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
Vicki
on 2 Dec 2025
This is the script - name it "script.m" and run it from the editor:
for i = 2:NumTimesteps % for each timestep
% 1. Soil Respiration % as per Reich, 2002
SoilResp_gC = SoilResp(Temp,Precip)
SoilResp_gO2 = UnitConvert(SoilResp_gC)
...
This is function SoilResp - name it "SoilResp.m" and save it in your working directory:
function SoilResp_gC = SoilResp(Temp,Precip)
SoilResp_gC = F.*(exp(Q*Temp)).*(Precip./(K+Precip));
end
This is function UnitConvert - name it "UnitConvert.m" and save it in your working directory:
function SoilResp_gO2 = UnitConvert(SoilResp_gC)
SoilResp_molesC = SoilResp_gC./12.01;
SoilResp_molesO2 = SoilResp_molesC;
SoilResp_gO2 = SoilResp_molesO2.*32;
end
Maybe you want to learn the MATLAB basics free of costs to get used to the new software:
Vicki
on 2 Dec 2025
Torsten
on 2 Dec 2025
Post the complete files and we will show you how to do it.
Steven Lord
on 2 Dec 2025
0 votes
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
Vicki
on 2 Dec 2025
Stephen23
on 2 Dec 2025
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.
Vicki
on 3 Dec 2025
Categories
Find more on State-Space Control Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!