Help adding external functions

79 views (last 30 days)
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
Anthony Koning
Anthony Koning on 11 Dec 2021
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 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.

Sign in to comment.

Accepted Answer

Image Analyst
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:
  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!

More Answers (1)

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

Categories

Find more on Environment and Settings 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!