Help adding external functions
86 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-12-11
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.
采纳的回答
Image Analyst
2021-12-11
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 个评论
更多回答(1 个)
Chunru
2021-12-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!