Help adding external functions

27 Ansichten (letzte 30 Tage)
Anthony Koning
Anthony Koning am 10 Dez. 2021
Beantwortet: Image Analyst am 11 Dez. 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 Kommentare
Walter Roberson
Walter Roberson am 11 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Dez. 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:
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)
function [a_m, b_m] = get_n_rates(Vm)
% Code....
function [a_m, b_m] = get_h_rates(Vm)
% Code...
% No more script lines after this!

Weitere Antworten (1)

Chunru am 11 Dez. 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.


Find more on Entering Commands 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!

Translated by