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?

  • 2
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Feb 3 at 14:00
  • 2
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Feb 3 at 14:11
  • $\begingroup$ One issue that comes to mind: is blah = {1, 2, 3} a Mathematica list, or a MATLAB cell array? $\endgroup$ Feb 3 at 14:26
  • $\begingroup$ 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. $\endgroup$
    – Gravifer
    Feb 3 at 15:27
  • $\begingroup$ You can just edit your question to include this. $\endgroup$ 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:

  Import["file.m", "Text"],


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

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

To get a list of all possible languages:

Information[Classify["ProgrammingLanguage"], "Classes"]
  • 2
    $\begingroup$ +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? $\endgroup$
    – Michael E2
    Feb 7 at 16:56
  • 4
    $\begingroup$ @MichaelE2 You can use the ClassPriors option (see updated). $\endgroup$ Feb 7 at 19:07
  • 1
    $\begingroup$ Great! Thank you very much. $\endgroup$
    – Michael E2
    Feb 7 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.