what is the meaning of this error? how to stop this error?

2 次查看(过去 30 天)
For context, I am using this as a calling function.
function [pa] = Forward_model(M,El_sp)
M = [220,208,70,6,5]; %[rho1 rho2 rho3 depth1 depth2]
rho = M(1:3); %resistivity of each layer
h = M(4:5); %thickness of each layer
%Forward Modelling
s = El_sp; %electrode spacing
m = length(rho);
T = zeros(1,3);
T(m)=rho(m);
lambda=zeros(1,19);
ar=xlsread('filterbasee.xlsx','A1:A19'); %base e
phi=xlsread('filterbasee.xlsx','B1:B19');
for i=1:length(s)
for r=1:length(ar)
lambda(r)=exp((ar(r)-log(s(i))));
for j=(length(rho)-1):-1:1
T(j)=(T(j+1)+rho(j)*tanh(lambda(r)*h(j)))/(1+(T(j+1)/rho(j))*tanh(lambda(r)*h(j)));
end
A(r)=T(1);
end
sum=0;
for m=1:19
sum=sum+(phi(m)*A(m));
end
pa(i)=sum;
end
pa=transpose(pa) %calculated apparent resistivity
end
Error: File: Forward_model.m Line: 5 Column: 17
Local function name must be different from the script name

回答(3 个)

KSSV
KSSV 2023-3-18
You have copied this function named 'Forward_model' under a script/ code whose file name is also same as the function i.e. Forward_model.m. Two options to remove the error.
You either change the name of the script file or change the name of the function.

the cyclist
the cyclist 2023-3-18
One reason for getting that error is having text in a line before the function definition. So if your function file looks like this
For context, I am using this as a calling function.
function [pa] = Forward_model(M,El_sp)
you should change that to
function [pa] = Forward_model(M,El_sp)
% For context, I am using this as a calling function.
  3 个评论
the cyclist
the cyclist 2023-3-18
I thought that might be the case.
Regardless, the problem is still that you have a file of the form
% Some lines of code here, which are part of a script.
rand(3)
% Function declaration here
function [pa] = Forward_model(M,El_sp)
and that that file is named Forward_model.m.
So, that file name is the name of the whole script (including the code before the function declaration), and MATLAB is telling you that you cannot name your function the same thing as the script.

请先登录,再进行评论。


VBBV
VBBV 2023-3-19
If you have function
% assuming the below line is not present as per your comments
% For context, I am using this as a calling function.
function [pa] = Forward_model(M,El_sp)
in a script file, saved with same name as function i.e. Forward_model then in order to execute the function, you need to call it from command window with same number of arguments
% command window
>> Forward_model([220,208,70,6,5],2)
%-> some values
instead of running the function using RUN button from editor. This is an example of command line function call execution. When you press RUN button from an editor window, to execute code, it will check for any code statements from beginning (line 1). If it begins with a key word function, with no other preceding lines of code (except comments) then it becomes a function file, otherwise it remains as script file.
To execute such a code, call the function using its name from a command line window. To know more about how to create functions in files and execute them, read this page
Usually script files have a different name other than function names within files, but when the file/s begin with keyword/s function then script fillename takes same name as function name . Then in such case, execute the code in file from command window

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by