matlab_commandline, a MATLAB code which illustrates how MATLAB can be used from the UNIX commandline, rather than within the usual MATLAB command window. There may be cases where this mode is more convenient (because you know what you want MATLAB to do, and you don't want to interact) or necessary (because you're logging into a remote system, and can't get X-Windows set up properly to transmit a command window).
Some issues that immediately arise include:
The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.
matlab_commandline is available in a MATLAB version.
matlab_calls_c, MATLAB codes which illustrate how MATLAB can call a C function, using the MEX facility.
matlab_compiler, MATLAB codes which illustrate the use of the Matlab compiler, which allows you to run a Matlab application outside the Matlab environment.
matlab_os, examples of how the system command can be used to allow MATLAB to issue commands to the computer operating system (UNIX or DOS);
In order to run MATLAB, you probably simply type
matlab
However, this only happens because matlab is in your path, that is, the executable program is in one of the directories that the your login shell told the operating system to search.
When you want to run MATLAB in batch mode, you may need to specify exactly where the MATLAB executable can be found. (This might not be necessary if you've set up your path carefully, but it's good to know in any case.) To identify exactly where the executable is, type
which matlab
The response should be something like:
/usr/local/bin/matlab
This is the "full path name" of MATLAB on your machine. And thus,
as an alternative, you can run MATLAB by typing the filename as a command:
/usr/local/bin/matlab
On a Macintosh computer, running OSX, you probably click on the MATLAB icon to start MATLAB. Where did the MATLAB icon come from? It was probably set up when you or your system administrator set up MATLAB on your machine.
In fact, clicking on the MATLAB icon sends a command to the underlying operating system, which knows where the MATLAB code is. If you can find your MATLAB executable, then you can execute it directly, from your commandline, with a written command, instead of an icon click.
To find where MATLAB has been hidden on a Macintosh system, move to your applications directory and ask for a directory listing:
ls /Applications
You should see a MATLAB directory in the listing, such as
MATLAB_R2018b.app
Your MATLAB executable program is probably in the bin subdirectory:
ls /Applications/MATLAB_R2018b.app/bin
Once you have determined the full path name of MATLAB on your Macintosh, you should be able to run MATLAB by typing this name as a command:
/Applications/MATLAB_R2018b.app/bin/matlab
Since this is rather much to remember, you can also try sticking an alias
in your .bash_profile:
alias matcom='/Applications/MATLAB_R2018b.app/bin/matlab'
so that the alias matcom can be used to invoke MATLAB.
MATLAB expects the user to issue a series of commands. MATLAB users are familiar with the idea of MATLAB "script" files, which are simply text files containing a series of commands to MATLAB that could have been entered interactively. A script file typically has a ".m" filename extension, such as "power_table.m".
The file power_table.m might consist simply of a computation of a table of squares and cubes, as follows:
fprintf ( 1, '\n' );
fprintf ( 1, ' N N-squared N-cubed\n' );
fprintf ( 1, '\n' );
for i = 1 : 10
fprintf ( 1, ' %10d %10d %10d\n', i, i^2, i^3 );
end
During an interactive session with MATLAB, the commands in the script are executed simply by invoking the script file's name:
>> power_table
N N-squared N-cubed
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
>>
You might suspect that part of the technique for running MATLAB in batch mode will be to redirect MATLAB's input stream to the M-file we have created, as in
/usr/local/bin/matlab < power_table.m
This is indeed part of the solution.
Note, however, that when we ran the M-file interactively, that even after
MATLAB carried out all the input commands in power_table.m, it still
expected more input. If we're not careful, MATLAB will simply
continue to run, expecting more input from us.
The key to using MATLAB in batch mode is this idea of an M-file. Presumably, though, the actual computation we want to carry out is more complicated than this simple example. We must prepare an appropriate M-file that completely defines this calculation, that is, that instructs MATLAB on all the tasks we need it to do, including where to find other input files, what to name the output files, and so on, all without any chance of interactive correction by us. As you can imagine, this can take some thought and patience.
This means that we will need to know precisely some things we often don't worry about, such as what our default directory will be, how we can create graphics, and how we can terminate the session with MATLAB.
If you simply issue the command
/usr/local/bin/matlab < power_table.m
then MATLAB starts up OK, and it knows where to get its output.
Of course there's output from these commands, and it has to go
somewhere. In fact, it goes straight to
standard output - in this case, to the user terminal. So
if you issue this command, you should see the table
of powers printed out. That's fine for a test, but probably not
what you want for your big batch computation!
Of course we know what to do - simply redirect the standard output (MATLAB's responses, that is) to a file:
/usr/local/bin/matlab < power_table.m > power_table_output.txt
This seems logical enough, but if you issue this command, you'll probably note that the program never seems to terminate. You may have to type CONTROL-C to kill it, and you may not actually have any output in the output file. That's because MATLAB, after reading all the input in power_table.m, doesn't terminate, it just waits patiently for you, just as it would in interactive mode.
We need to tell MATLAB when it's time to die!
When you want to end an interactive MATLAB session, you probably go up to a pull-down menu and select QUIT. However, we'll have no access to menus in a batch job. Instead, to get MATLAB to terminate the session, we need to use the exit command.
So if we add an exit statement as the last line of our power_table.m script, we can run it as follows:
/usr/local/bin/matlab < power_table.m > power_table_output.txt
It should be the case that this program begins, runs, and terminates, all in an orderly fashion. The output file includes our desired results (plus some other junk).
< M A T L A B >
Copyright 1984-2006 The MathWorks, Inc.
Version 7.3.0.298 (R2006b)
August 03, 2006
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> >> >> >> >> >> >> >> >> >> >>
>> N N-squared N-cubed
>>
>> 1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
>> >>
However, you probably also noticed, after you issued the command, that a MATLAB X-Window showed up, with the MATLAB logo. This is because MATLAB doesn't quite realize that we're only interested in a batch session. It's establishing an X-Window connection to the current display. For now, this is a minor annoyance, but we'll actually want to suppress this behavior entirely.
On a UNIX system, MATLAB uses the X-Windows Interface to interact with the user. In a batch execution, this communication is not necessary.
We don't need the initial "splash" which displays the MATLAB logo. We can turn this off with the -nosplash option.
We also don't want the interactive menu window to be set up, which can be suppressed with the -nodisplay option
Two other options are available which may be useful in suppressing visual output and logs, the -nojvm option ("no Java Virtual Machine") and -nodesktop.
Now our statement to run a batch job might look like this:
/usr/local/bin/matlab -nosplash -nodisplay -nojvm -nodesktop < power_table.m > power_table_output.txt
Of course, you can experiment to see which options are necessary. I seem to be able to use the simpler commands:
matlab -nosplash < power_table.m > power_table_output.txt
When you use MATLAB as a simple calculator, you may not care that MATLAB defines a working directory, which is the default location where files will be created, and where certain things like user M-files will be looked for.
If you have used M-files, you know that the easiest way to access them is to "move" MATLAB's current working directory to the directory containing your M-files; of course, you could also modify MATLAB's "path", or even write out the full directory information needed to find the file you are referring to.
When using MATLAB in batch, however, you'll probably need to know
To find out where MATLAB sets its default working directory, simply start the program up in interactive mode, and issue the command pwd. This directory is the same place that your MATLAB batch jobs will start out in.
To change the default working directory (once and for all), you need to create a file called startup.m with the appropriate command, and place this file somewhere in the MATLAB path (which you may or may not be able to do!).
The appropriate command to put in startup.m is simply a cd command that moves to the directory where you want to work.
cd /Users/burkardt/public_html/m_src/matlab_batch
make sure
that it is placed somewhere in the MATLAB path.
If the startup.m file cannot be used to reset the default initial working directory, you just have to insert a similar cd command in the main M-file that you want to execute in batch. When you explicitly move MATLAB's working directory, you gain the ability to easily access all the M-files in that directory, and to know how to access files you wish to read or write.
Despite the fact that we've suppressed the visual interface interface to MATLAB, we can still generate graphics...we won't see them of course, unless we are careful to save them to files.
Perhaps the simplest example of a MATLAB graphics command is the basic plot(x,y) command that makes a line plot of data. So now let us suppose that our M-file generates some data, as before, but rather than printing it, or writing it to a text file, we wish to create a graphics file using the plot command.
One way to do this is simply to follow the plot command by a version of the print command that "prints" the image to a graphics file:
print ( '-dpng', 'plot.png' );
Here the -dpng switch specifies that we wish to create
a PNG graphics file, and the second argument is simply the name
of the file to be created. The graphics file created will be
stored in the current working directory.
table_script prints a table of powers of integers.
file_script writes a table of integer powers to a file.
plot_script plots integer powers, and saves the image to a file.