unable to define local function because it has the same name as the file

336 ビュー (過去 30 日間)
Elprololo
Elprololo 2018 年 2 月 13 日
編集済み: Stephen23 2021 年 8 月 11 日
Seriously, what kind of programming language consider this is an error ?
Is there only a way to overcome this problem ? I though that in matlab function where forced to be set in files that have the same name.
I am desperate about this software, why would someone use something like matlab when there are great opensource tools in python out there.
  2 件のコメント
Dayi Li
Dayi Li 2019 年 6 月 15 日
Check your file and see if there is any extraneous code before the line defining the function. That might be the problem.
per isakson
per isakson 2019 年 6 月 15 日
Note: Including functions in scripts requires MATLAB® R2016b or later.

サインインしてコメントする。

回答 (3 件)

James Browne
James Browne 2019 年 6 月 15 日
I believe the answer is that you can have an entire script be a function, if the file name is something like "solverX" and the first line of the file is:
function solverX(input)
Then the entire script behaves as a function and you can call it from any other file in the same working directory, simply by:
y = solverX(argument);
This ability is actually quite handy when you have dozens of custom functions that you may want to be able to call at any time from within any given file. It may be frustrating to learn at first, but I actually started using my files as functions a long time ago, it really helps me keep things organized.

DanielFromIllinois
DanielFromIllinois 2021 年 8 月 11 日
編集済み: DanielFromIllinois 2021 年 8 月 11 日
I had this error pop up and it was caused by some accidentally uncommented code above the function call. The uncommented line was erroneous itself too. Properly commenting it out removed the error. Similar to what @Dayi Li said on this post. Adding this solution because it is what helped me fix it. As for why this is the error text for this specific issue is beyond me. I'm not sure what the developers were getting at or assuming someone was trying to erroneously do?
  2 件のコメント
Kishore S Shenoy
Kishore S Shenoy 2021 年 8 月 11 日
Your comment seems cryptic. Care to elaborate?
Stephen23
Stephen23 2021 年 8 月 11 日
編集済み: Stephen23 2021 年 8 月 11 日
"As for why this is the error text for this specific issue is beyond me."
Simply because you wrote a script (exactly as Dayi Lu wrote two years ago). A script is identified by its filename... and within your script you also defined a local function using the script name. Thus the error message quite accurately informs you that the local function has the same name as the filename (i.e. script).
You wrote a script.
You wrote a local function.
They had the same names (which is not allowed).
As far as I can tell, the error message includes that information.
Given that MATLAB cannot distinguish between code intentionally added (i.e. the user wants to make a script) vs. your "accidentally uncommented code" that turned it into a script, what error would you suggest to inform the user that their local function uses the same name as the script they have just written ?

サインインしてコメントする。


Soumya Sinha
Soumya Sinha 2019 年 6 月 17 日
You can create a separate SCRIPT for each and every function name and this created script could be used from any other script.
The standard syntax for function definition is as follows
function [outputArguements] = functionName(inputArguements)
%Whatever written here is executed when the function is called from a script
end
You can read more about matlab functions, their definitions, working but clicking on the link MATLAB functions

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by