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

조회 수: 20 (최근 30일)
bribee
bribee 2017년 6월 8일
답변: Jan 2018년 11월 8일
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
Julian Behrens
Julian Behrens 2018년 11월 7일
@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 2018년 11월 7일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 11월 8일
"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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by