3

I want to run the Mathematica file with some extension, like '.m'. But, I don't know, how can I run the code. Please, guide me.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
MMS
  • 87
  • 7
  • 4
    Have a look at Get. – Henrik Schumacher Mar 17 '19 at 12:25
  • To keep it simple, pass the full path to the file to the Get function. That's all. – Szabolcs Mar 18 '19 at 12:12
  • what is ''Get''? There is no link. I can't open this. How can I pass the full path to the file? Can you help me Please? I have Mathematica 11.3 in my system. But, not able to run the code in file with extension ''.m''. – MMS Mar 18 '19 at 13:26
  • Please help me. Actually I don't know about packages. So, I am facing difficulties. – MMS Mar 18 '19 at 18:17
  • 1
    Actually, corresponding with OP and checking the *.m file, it is clear from the contents of the file that it is not generated by Mathematica, and is not of Wolfram syntax (hence the close vote). Perhaps the *.m file is from Matlab, as I recall its vector syntax being [v1, v2, ...] that complies with said file. – István Zachar Apr 05 '19 at 16:38

1 Answers1

2

If a file has the extension .m, then I assume you are trying to load a package into your notebook. This can be achieved by calling the package (say called PackageName.m) into your own notebook by doing the following:

  1. You can type into your opening like <<PackageName`, which uses the backtick symbol `. It is probably at the left hand side of your keyboard.

  2. Alternatively, this is a shortcut for the MMA function Get, given in the documentation here. The first example has the above notation I was talking about. Then Get["PackageName.m"] would work.

Here is a screenshot so you can see the backtick in action.

Click on this to take you to my image.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Brad
  • 135
  • 13
  • Thank you so much for your kind response.
    Are you talking about this symbol '' ~ ''. I just type "<<PackageName(~)" in the notebook, but it does not work. I also type ''<<~'', it also does not work. Sorry to say, I am facing difficulty to learn from the example.
    – MMS Mar 17 '19 at 14:13
  • I have added a screenshot. You are on the right key I think, but instead you have held-down shift instead. It's a tricky key to type, but my screenshot should help you. – Brad Mar 17 '19 at 14:17
  • 1
    It's tricky but: enclose any code that has backticks in double backticks. I did this for your answer. – J. M.'s missing motivation Mar 17 '19 at 14:23
  • @J.M.isslightlypensive that's one of the worst things that to look at - 5 backticks is far too many. Thanks for your help JM. – Brad Mar 17 '19 at 14:24
  • Now, I followed your suggestions and type. But, I found the error. I am pasting here both input and output which I got. In[8]:= <<PackageName` Get["PackageName.m"]

    During evaluation of In[8]:= Get::noopen: Cannot open PackageName`.

    Out[8]= $Failed

    During evaluation of In[8]:= Get::noopen: Cannot open PackageName.m.

    Out[9]= $Failed

    – MMS Mar 17 '19 at 15:27
  • @MisbahShahzadi - unless you've called your package PackageName then Mathematica won't find it. You need to search for your own package name which you have on your system. A key point is to make sure that the package is installed in the correct directory. This either could come with a file distributed with your package, which will automatically install it in the correct location, or you'll need to do it manually. A buzzword might be $UserBaseDirectory. This post may be useful: https://mathematica.stackexchange.com/questions/669/how-to-install-packages – Brad Mar 17 '19 at 15:30
  • Thank you so much. I tried with the same procedure as mentioned in the link but get syntax error. How can I paste here screen shot?, so you can see the error. – MMS Mar 18 '19 at 18:21
  • 1
    Screenshots aren't much use. If you have to, post your screenshot and more importantly, your code as an edit in your original post. – Brad Mar 18 '19 at 18:25