how to access function from a different scipt?

7 views (last 30 days)
i keep getting an error that says "Local function name must be different from the script name." when i try to call a funtion from a different script
i know my funtion works i just dont know how to call if from a different script
clear all
close all
create_array(10,5,1)
function create_array(N,M,row_orient)
if row_orient==1
for i=1:N
for j=1:M
if N==M
x(i,j)= N*(i-1)+j
elseif M-N >=1 | N-M >= 1
x(i,j)=M*(i-1)+j
end
end
end
elseif row_orient==0
for i=1:N
for j=1:M
if N==M
x(j,i)= N*(i-1)+j
elseif M-N >=1 | N-M >= 1
x(j,i)=M*(i-1)+j
end
end
end
end
end

Accepted Answer

Steven Lord
Steven Lord on 4 Sep 2020
That error message is correct. If you have a function inside a script file, the name of the function must not be the same as the name of the script. This is documented in the Note in the "Syntax for Function Definition" section on this documentation page.
As stated on this other documentation page functions in script files are not normally accessible outside that script file. If you want that function to be accessible one of the following must be true. [I don't think I missed any possibilities, but if I did these are the most common ways to make a function in a file accessible to the Command Window and other files.] The first is probably the easiest.
  • The function needs to be the main function in a function file (not a script file)
  • The script file needs to create a function handle to the function
  • The function file in which it is a local function needs to return a function handle to that local function, or
  • The function needs to be a method of a class
  2 Comments
tyler hollings
tyler hollings on 4 Sep 2020
ok dumb question what is a funiton file and how do i make it? ive only ever used script files and creating a funtion file seems like the easiest thing to do
tyler hollings
tyler hollings on 4 Sep 2020
never mind i figured out how to make the funtion file thank you steven lord!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!