Clear Filters
Clear Filters

how to properly call a function in a separate m-file?

209 views (last 30 days)
I have created the following function in a separate m-file called "fourierCompute.m"
function [Ck, Basis, t, x] = fourierCompute(tp, xp, delta_t, t_begin, t_end, T, N)
k = -N:N;
w0 = 2*pi/T;
Basis = exp(1i * w0 * k.' * tp);
Ck = (1/T) * int(xp * (exp(-1i * w0 *k * tp)), tp, -T , T);
t = t_begin:delta_t: t_end;
x = Ck * Basis;
end
In a separate m-file I am trying to call that function, but when I run it I get the messge "Undefined function or variable 'fourierCompute'." Both files are in the same location so I am not sure what is wrong. How do I properly call this function into a separate m-file? What am I doing wrong?
[Ck, Basis, t, x] = fourierCompute(tp, xp, delta_t, t_begin, t_end, T, N);
  4 Comments
Julian Behrens
Julian Behrens on 7 Nov 2018
@dbp I added the folder in which the .m file is, which contains the function I want to call. I want to call the function from another file in the same folder (so I guess same wd), but still I get the message of "Undefined function or variable ...". Why is that? What can I do?
dpb
dpb on 7 Nov 2018
Check spelling of both the file and the directory in the MATLABPATH
Only the primary function in an m-file has scope outside the m-file itself so if the one wanted to be called were a local or nested function, it will not be visible to an external function.

Sign in to comment.

Answers (1)

Jan
Jan on 8 Nov 2018
"Undefined function or variable 'fourierCompute'."
This means, that the fourierCompute.m is not found in the path or the current working directory.
Relying on the current working directory is prone to bugs. Remember that each callback of a GUI or a timer can call cd to change the folder unexpectedly. Prefer to use addpath(folder, '-end') or pathtool to insert the parent folder of the .m file to the path.
Check the existence of the file in the path:
path
which fourierCompute.m -all

Categories

Find more on Startup and Shutdown 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!