why matlab won't support file name length more than 63 char?
127 views (last 30 days)
Show older comments
Is there any limitations with respect to operating system, controllers or anything?
Please can someone explain me clearly what could be the possible reason?
1 Comment
Henric Rydén
on 22 Jun 2015
I have never heard of this issue. I tried creating a folder with 115 characters in it and it worked.
Accepted Answer
Jan
on 22 Jun 2015
Edited: Jan
on 22 Jun 2015
No, Matlab supports longer file names. Some commands are limited by the file system, e.g. deleteinmg files to the recycle bin under Windows suffers from the horrible limit of 260 characters for the path name. But for a simple file copy etc. wide names are accepted, which allows 32767 characters. (Please feel free to send a letter to Bill Gates and ask him, why the 260 characters limit is still prsent in teh 2015'th Windows 10.)
Under Linux/MacOS long file names are supported in general.
But the names of variables are limitd to 63 characters. Perhaps this is your question. This limit is useful, because longer names for variables are ridiculous and cannot be read or debugged anymore. Such long names are a secure hint, that the programmer hides information in the name of the variable. But such information should be found in the data, in the values of the variables, e.g. by using structs with an extra field for these information.
As the names of variables, the names of functions are limitted als for the same reason.
Beside the readability and the idea of smart code design, the symbols can be managed more efficiently, if the length of the names is limited. E.g. the fieldnames of a struct can be stored in blocks of a fixed width: For each fieldname 64 bytes are reserved (63 characters plus a trailing \0). This allows to find the n'th field name without considering the lengths of the former n-1.th names. In consequence the lookup tables for finding variables in the workspace or fields in a struct can be processed faster, when the length of the names is limited.
The limit was 31 characters until Matlab 5.3.
20 Comments
Walter Roberson
on 10 Nov 2016
Yes, system names must be MATLAB identifiers. Simulink models are callable by name in MATLAB so they have to meet the identifier requirements
David J. Mack
on 10 Oct 2022
I disagree with the "are ridiculous and cannot be read" part. When writing unittests it is utterly important to make them self-explanatory. A usual naming scheme consists of three parts - the name of the unit-under-test (aka function), the precondition (what am I doing?) and the expected results (what is supposed to happen; not the same as what is supposed to be returned!). In this case, you often have longer names, which are actually very helpful for understanding & debugging. And the length limitation actually enforcers developers to make shortened names which often end-up being so cryptic, that you cannot really derive anything from it.
So yes, make variable names that long might not make too much sense. But for functions this isn't true.
More Answers (1)
Steven Lord
on 22 Jun 2015
If you want to create a MAT-file with an extremely long name you can do that, as MAT-file names is not one of the categories listed in that documentation. If you want to create a variable or a script file with an extremely long name, you will receive a warning and the name will be truncated.
I agree with Jan's assessment -- if you are trying to create a variable with a 70, 80 character name you're probably trying to encode some information in it that is more appropriate to store as data rather than as part of the name. For that I'd consider a separate variable or storing the two pieces of data in a struct array.
8 Comments
Adam
on 7 Aug 2018
I can't actually find the section on packages in the Matlab help apart from this one which focuses on classes, but it is even simpler for functions.
Simply create a folder with a '+' at the front of its name and that becomes a package e.g.
+engine_temperature_model
Then inside that folder you create files called:
left_sensor_measurement_no_lowpass.m
right_sensor_measurement_10min_lowpass.m
etc
And when you refer to them in code or call them on the command line you refer to them as e.g.
engine_temperature_model.left_sensor_measurement_no_lowpass.m
i.e. the package name followed by '.' followed by the name, so the final thing is very similar to your long name, but you syphon off that common prefix into a package name.
I ebb and flow on whether or not I like packages. Since I write mostly classes I recently ended up using packages for inheritance which leads to the bizarre situation of me having loads of files with exactly the same name because the descriptive part that differs (the specialisation) is in the package name now rather than the filename, so when I open them in the editor they just all show up with the same name! They can still be useful for collecting together code though, and if you have a long scope where you refer to many functions from the same package you can just put e.g.
import engine_temperature_model
at the top and then you can just refer to each by its filename without needing to constantly prefix the package name.
See Also
Categories
Find more on Naming Conventions 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!