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 个评论
Anthony Koning
Anthony Koning 2021-12-11
I am currently working on a project where my professor wants us to have a few specific functions listed externally, something about making debugging easier. He said that having the files within the same folder as the listed names would allow them to be retrieved externally, though not sure how exactly. The demonstration code he has lead with this:
% External Functions:
% get_n_rates(Vm) get_m_rates(Vm) get_h_rates(Vm)
where get_n/m/h_rates are files saved to the same location and root. The get rate scripts run without issue, and plugging them directly into the main project with the External Functions code allows it to work without issue, but Im curious how he managed to get them to work. I believe he said something about needing to convert from a script to a function, though I am unsure of how to do this. Using get_m_rates(Vm) as an example, scripting "[a_m, b_m] = get_m_rates(Vm)" leads to an unrecognized function/variable error message, and scripting "function[a_m, b_m] = get_m_rates(Vm)" leads to an error message of "Function definition are not supported in this context. Functions can only be created as local or nested functions in code files."
For context, the get_m_rates(Vm) code works fine, and is as follows:
v = 0:5:50
a_m = (-.1.*(v+35))./(exp(-(v+35)./10)-1)
b_m = 4.*exp(-(v+60)./18)
Thanks for any help
Walter Roberson
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
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:
  1. If a function is defined in the script, it must come after the script, and
  2. the function must have a closing "end" statement, and
  3. 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!

更多回答(1 个)

Chunru
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.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by