how to access function from a different scipt?

1 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2020-9-4
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 个评论
tyler hollings
tyler hollings 2020-9-4
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 2020-9-4
never mind i figured out how to make the funtion file thank you steven lord!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by