# How to determine the file type of a .m file?

Both Mathematica and MATLAB widely uses .m files, but with diametrically different syntaxes and usage. Currently WRI is moving towards the .wl extension, however due to compatibility reasons .m files aren't going anywhere soon.

Suppose I receive a .m file from a collaborator without much context, how shall I determine its correct association based on its content?

• Here are a few ideas: 1) inspect the file yourself; 2) try to load it in MMA and see if it works; 3) email your collaborator back to ask. Unless you receive tens of these files a day, anything but a manual approach seems overkill. Feb 3 at 14:00
• Is this motivated by a real practical problem? If so, what is the actual problem? If you've ever worked with either Mathematica or MATLAB, even very briefly, it will be trivial for you to see if the file contains (or does not contain) Mathematica/MATLAB code. If you have never seen either MATLAB or Mathematica code, why would someone send you such a file with no explanation? There would be far bigger obstacles to making use of that file than just deciding which language it is written in. Feb 3 at 14:11
• One issue that comes to mind: is blah = {1, 2, 3} a Mathematica list, or a MATLAB cell array? Feb 3 at 14:26
• I was about to explain my intention with a comment but forgot it at all. I am a MMA user, collaborating with both MMA and MATLAB users. I am thinking how to write a simple shell tool to distinguish MMA and MATLAB files, therefore I asked this question. Feb 3 at 15:27
• You can just edit your question to include this. Feb 3 at 15:30

It's somewhat of a meme that WL has a top-level function for everything, but it's kinda true:

Classify["ProgrammingLanguage",
Import["file.m", "Text"],
"TopProbabilities"
]


# Edit

To force Classify to pick between two options, you can use the ClassPriors option:

Classify["ProgrammingLanguage",
Import["file.m", "Text"],
"TopProbabilities",
ClassPriors -> <|
Entity["ProgrammingLanguage", "WolframLanguage"] -> 0.5,
Entity["ProgrammingLanguage", "MATLAB::82q2f"] -> 0.5
|>
]


To get a list of all possible languages:

Information[Classify["ProgrammingLanguage"], "Classes"]

• +1. It classifies several of my MATLAB files as Python, one as Tcl. It suggested Perl or Scala for a short Mathematica .m file. Overall, it's more reliable on longer files, of course. Is there an easy way to limit its choices to pick which of WL / Matlab is most probable? Feb 7 at 16:56
• @MichaelE2 You can use the ClassPriors option (see updated). Feb 7 at 19:07
• Great! Thank you very much. Feb 7 at 19:18