Help adding external functions
79 views (last 30 days)
Show older comments
Anthony Koning
on 10 Dec 2021
Answered: Image Analyst
on 11 Dec 2021
Whenever I try to add an external function to my script (written as "external functions: X, Y, Z"), my script keeps syaing that these functions are unrecognized, despite the scripts working on their own even when plugged directly into the main script. If anyone knows how to get these external scripts to run, I would appreciate it
4 Comments
Walter Roberson
on 11 Dec 2021
Lines beginning with % are just comments. That bit about External Function is there as documentation.
function statements cannot be executed at the command line.
If you have an old enough version of MATLAB, then functions cannot occur inside of "script" files either. Files that are .m files that do not start with the word function or classdef are considered "script" files.
Accepted Answer
Image Analyst
on 11 Dec 2021
Normally, if done right, this would work
[a_m, b_m] = get_m_rates(Vm)
in your script as long as get_m_rates is either a separate, second m-file located somewhere in your search path, OR defined in your script's m-file.
Because you're getting the error message of "Function definition are not supported in this context. Functions can only be created as local or nested functions in code files." that tells me you probably have your script followed by a function definition in the same m-file. There are some rules about that:
- If a function is defined in the script, it must come after the script, and
- the function must have a closing "end" statement, and
- after that closing end statement you cannot startup with script lines again.
So essentially the m-file would look like
% Script starts here:
clc;
Vm = whatever;
[a_m, b_m] = get_n_rates(Vm)
[a_m, b_m] = get_m_rates(Vm)
[a_m, b_m] = get_h_rates(Vm)
% Script ends here, and function definitions begin:
%============================================
function [a_m, b_m] = get_m_rates(Vm)
v = 0:5:50
a_m = (-.1.*(v+35))./(exp(-(v+35)./10)-1)
b_m = 4.*exp(-(v+60)./18)
end
%============================================
function [a_m, b_m] = get_n_rates(Vm)
% Code....
end
%============================================
function [a_m, b_m] = get_h_rates(Vm)
% Code...
end
% No more script lines after this!
0 Comments
More Answers (1)
Chunru
on 11 Dec 2021
MATLAB has no external function like c and other languages. It relies on "path" to figure out the external functions. You can still put a comments for external functions to indicate that the current function relies on some other functions.
MATLAB also has more advanced features for managing the data and functions for a project. Search "Projects" in dcouments for more info. If you want to put your software into a better management, refere to class and packages.
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!