4
$\begingroup$

I have been using the Notebook in Mathematica for quite a while. While I like its interface, the problem is successive statements sometimes get read into different Inputs, and when I am rerunning the file, I need to run all the Inputs sequentially. Correspondingly separate outputs are produced. For a long file, running Inputs one by one is time consuming.

I would like to have a Matlab-script environment, where I enter the code completely, and print the outputs to be displayed at the end. Executing the script once will get me all the outputs consecutively in one place.

Is such an inputting possible for Mathematica. Apologies if the question is too naive and easy.

$\endgroup$
6
  • 1
    $\begingroup$ I don't know matlab very well so I might not know what you are looking for, but you can put a series of commands and definitions inside a text file, "myProgram.m" and then run every expression therein using Get like <<"myProgram.m" $\endgroup$
    – Jason B.
    Jan 12, 2017 at 14:56
  • 9
    $\begingroup$ Maybe I misunderstand you, but why not write all the code in one cell? $\endgroup$
    – xzczd
    Jan 12, 2017 at 15:04
  • 4
    $\begingroup$ Quite likely, but it would help if you were somewhat more specific about what you want. Do you want the output printed to a Notebook, or to a separate output text file? Do you want to write the input code in plain text or as part of a fully featured Notebook? $\endgroup$
    – Mr.Wizard
    Jan 12, 2017 at 15:04
  • $\begingroup$ I suggest you to write a Block that runs your Matlab code through a Unix command. I've done so with a Fortran code and avoided having to do translations or other adaptations. $\endgroup$ Jan 12, 2017 at 15:08
  • 2
    $\begingroup$ xzczd option is good. Another one is Evaluation->Evaluate notebook. Another is to use the command line , something like math.exe < my_script.m > my_output.out $\endgroup$
    – Nasser
    Jan 12, 2017 at 21:57

1 Answer 1

4
$\begingroup$

I don't know if you are aware of the some features of Mathematica.

If not they could be of great help for you.

It is related to the way you can write and regroup some scripts (I don't know matlab, so no comparaison possible)

Say you have severals scripts you have developped in small cells :

script 01 :

Print["hello world"];
1+1

hello world
2

script 02 :

 Print["how are you ?"];
 1+2  

how are you ?
3

First thing you can do is to set the options "Initialisation Cell" to the input cells (in blue), so they are automaticaly executed at Mathematica startup.

But maybe you only want to give a name to your scripts but don't want to execute them at Mathematica startup. Here, the idea is to execute these scripts by "calling" there name. (I suppose this is what you do with Matlab). Here is how to do this :

script01:=(
    Print["hello world"];
    1+1  
)  

When you evaluate the cell above, apparently nothing happen, but in fact script01 is memorized by Mathematica. To execute this script, just write script01 in a new cell and evaluate it :

script01  

hello world
2

So a friendly procedure to develop some small pieces of scripts that are intended to become a large one is :

  • write and test small pieces in small cells. The results of the tests appears just under the cell.
  • give a name to the script as explain above.
  • if needed you can test it (or a modified version) simply by calling his name, either in the cell that define the script or in a new cell (see (1) to understand why it can be interesting to use a new cell)
  • set the option "Initialisation Cell" in the cell that contain the script definiton.

Then each time you call Mathematica, the definition of the scripts are automaticaly evaluated. You can call the scripts by their name everywhere you want, in particular you can group them in a single cell, the only one you have to evaluate to execute your menu of scripts:

script01

 script01:=(
    Print["hello world"];
    1+1  
) 

Test :

script01

"hello world"
2

Test OK

script02

 script02:=(
    Print["how are you ?"];
    1+2  
)   

Test :

script02  

how are you ?
3

Test OK.

Menu :

script01
script02

hello world
2
how are you ?
3

(1) According to what is explained after : Simply because if do not want that your script is executed at Mathematica startup, use a separate cell

NOTE I use words like "script", "name", "calling" to be understable. In reality these words doesn't exist in Mathematica's world because they are included in the concepts of "expression" and above all "evaluation" in their generality (they are even not special cases)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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