What are the rules for naming script files?

78 ビュー (過去 30 日間)
Gurudatha Pai
Gurudatha Pai 2012 年 2 月 24 日
編集済み: John Kelly 2014 年 5 月 27 日
Hello,
This is perhaps one of those "I-know-the-answer-still-I-ask" questions. Today, I had a strange error in one of my scripts.
??? 17McModel
|
Error: Unexpected MATLAB expression.
The name of the script is "17McModel.m". After a lot of struggling to figure out the error, I realized that Matlab script files names cannot start with a number. I had rename my source files through windows! (Friday exercise of organizing the work folder!)
I tried to look for more information on naming rules for Matlab script and function files. In the Matlab documentation the only place that has any information is,
Matlab > User's Guide > Programming Fundamentals > Programming Tips > Files and Filenames > Naming Functions
and that is it.
Could anybody share more information and tricks-of-trade if you will. Like, what works and what does not, what naming conventions may be faster among other things.

採用された回答

Walter Roberson
Walter Roberson 2012 年 2 月 24 日
The rules are exactly the same as for variable names: start with a letter, followed by letters or numbers or underscore, maximum 64 characters (excluding the .m extension), and must not be the same as any MATLAB reserved word.
Any timing difference between different file names (that meet the rules) are so small as to be ignorable.
There have been many a flame war fought over file naming conventions, such as "camel case" vs underscores to separate words, whether the names should start with a verb followed by an object ("sort_ledger") or the other way around ("ledger_sort"), whether data-type hints should go at the beginning or the end, and so on. People have quit high-paying jobs over such matters.
None of the conventions matter to MATLAB itself: they only matter to the people writing the code, and the people maintaining the code (usually a much harder task), and to the people paying for the code (you'd be amazed how much gets written into contract specifications.)
  2 件のコメント
Andrew Newell
Andrew Newell 2012 年 2 月 24 日
I'm partial to camel case, but I can't imagine quitting over the issue!
James Tursa
James Tursa 2012 年 2 月 25 日
63, not 64. I think this is a holdover from the days when a MATLAB variable internally stored its own name within the variable structure itself using a char array of length 64, and one of those was reserved for the null character at the end of a C-style string.

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

その他の回答 (1 件)

Andrew Newell
Andrew Newell 2012 年 2 月 24 日
Check out Variables and the documentation for isvarname.

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by