how to call a function in a different script in matlab

In MATLAB, you can call a function defined in a different script by using the function name preceded by the script name or file path where the function is defined.

Assuming there's a function called myFunction in a script called myScript located in the same folder as the current script, you can call that function by simply typing:

main.m
output = myScript.myFunction(input);
37 chars
2 lines

Alternatively, if the script file is located in a different folder than the current script, you can either add the folder to your MATLAB search path, or include the file path when calling the function. For example:

main.m
addpath('/path/to/myScript/folder');
output = myScript.myFunction(input);
74 chars
3 lines
main.m
output = myScript.myFunction('path/to/myScript', input);
57 chars
2 lines

Note that if the function you want to call is defined inside a namespace (i.e. preceded by the function keyword), you need to specify the namespace as well. For example:

main.m
output = myNamespace.myScript.myFunction(input);
49 chars
2 lines

gistlibby LogSnag