Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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);
ar=xlsread('filterbasee.xlsx','A1:A19'); %base e
for i=1:length(s)
for r=1:length(ar)
for j=(length(rho)-1):-1:1
for m=1:19
pa=transpose(pa) %calculated apparent resistivity
Error: File: Forward_model.m Line: 5 Column: 17
Local function name must be different from the script name

Risposte (3)

KSSV il 18 Mar 2023
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 il 18 Mar 2023
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 Commenti
the cyclist
the cyclist il 18 Mar 2023
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.
% 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.

Accedi per commentare.

VBBV il 19 Mar 2023
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


Scopri di più su Workspace Variables and MAT-Files in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by