# Knowledge Base/MATLAB/General

### From Thalesians

## Unable to read/compare numbers displayed by MATLAB

`format` sets the output format. I usually use the following:

format long g

This sets the best of fixed or floating point format with five digits.

Note that `format` affects the way the numbers are displayed; it does not affect the way the computations are performed.

## Launching M-files from the command line

This is particularly useful for automating M-files' execution using batch files. Assuming the M-file is called `mycommand.m`.

matlab -logfile mylog.log -nosplash -nodesktop -r mycommand

This command will make a copy of any output to the command window in the logfile mylog.log. This includes all crash reports. It will suppress the splash screen during MATLAB's startup (-nosplash). The MATLAB desktop will not be displayed (-nodesktop); a simple V5 MATLAB command window will be used for all commands. -r mycommand specifies the M-file to be executed. Note that the M-file extension is omitted.

The disadvantage of this approach is that the command window will appear in the foreground. This may be undesirable. Replacing -nosplash -nodesktop with -automation will ensure that the command window is minimized:

matlab -logfile mylog.log -automation -r mycommand

There is a difference between the -nosplash -nodesktop mode and -automation. If you use -nosplash -nodesktop, MATLAB's current directory will be set to the shell current directory. -automation resets the current directory to `c:\progra~1\matlab\r2006b\bin\win32` or wherever your MATLAB is installed. Therefore you will not be able to run mycommand if it is in the current directory and that directory does not appear as part of the MATLAB path. This is easily resolved by modifying the path dynamically:

matlab -logfile mylog.log -automation -r "addpath h:\mycode; mycommand"

(Assuming `h:\mycode` contains `mycommand.m`.)

One problem remains. MATLAB will not close the command window on completion. To ensure the command window is closed when the execution is done, use

matlab -logfile mylog.log -automation -r "addpath h:\mycode; mycommand; exit"

## Long lines

If a statement does not fit on one line, use an ellipsis ("...") to indicate that the statement continues on the next line:

s = 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 ... - 1/8 + 1/9 - 1/10 + 1/11 - 1/12;

This works both in the Command Window and in the Editor.

## "Call by value" or "call by reference"?

Does MATLAB pass parameters using "call by value" or "call by reference"? What about passing structures or cell arrays?

You can find the technical details here. You may also wish to read this.

## Merging cell arrays

This can be achieved as follows:

>> arr1 = {'one' 'two' 'three'; 'four' 'five' 'six'} arr1 = 'one' 'two' 'three' 'four' 'five' 'six' >> arr2 = {'seven' 'eight' 'nine'; 'ten' 'eleven' 'twelve'} arr2 = 'seven' 'eight' 'nine' 'ten' 'eleven' 'twelve' >> [arr1 arr2] % Merging horizontally ans = 'one' 'two' 'three' 'seven' 'eight' 'nine' 'four' 'five' 'six' 'ten' 'eleven' 'twelve' >> [arr1; arr2] % Merging vertically ans = 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' 'ten' 'eleven' 'twelve'

The result in each case is a cell array.

## Deleting rows (columns) from cell arrays

This can be done as follows:

>> arr = {'red', 'orange'; 'yellow', 'green'; 'blue', 'indigo'; 'violet', 'white'} arr = 'red' 'orange' 'yellow' 'green' 'blue' 'indigo' 'violet' 'white' >> arr(2,:) = [] arr = 'red' 'orange' 'blue' 'indigo' 'violet' 'white' >> arr(:,1) = [] arr = 'orange' 'indigo' 'white'

Note that I am using *parentheses*, (), not *braces*, {}.

## Maps in MATLAB

There are two ways of implementing maps in MATLAB.

### Method 1: Native MATLAB

MATLAB provides a mechanism of dynamic field names which can be used to implement maps:

>> name = 'paul'; >> map.(name) = 24; >> map.('paul') ans = 24

### Method 2: Using Java

You can also use the familiar methods of the java.util.* package:

Method 1 requires the key to be a string. Method 2 does not. However, it is probably more desirable to build your interface around native MATLAB data structures rather than require your users to pass in Java objects to MATLAB functions. It is worth comparing the performance of the two methods.

## Invisible figures in MATLAB

If you execute the following code...

...a figure window will pop up and the user will see it as soon as the line

is executed.

You may wish to exercise more control over the figure window's visibility. Indeed it is straightforward to hide the figure. Use the following code instead:

You can then show or hide the figure window at will using, respectively,

and

## Animation in MATLAB

The following code is sufficiently self-explanatory.

On its own, MATLAB can display the resulting movie, but it cannot save it as an MPEG (although it does support other formats). `MPGWRITE` is a third-party addition to MATLAB that takes a MATLAB movie matrix and writes the move to disk as an MPEG file.

movie_rerun_count = 3; frames_per_second = 2; x = -1.0:0.1:1.0; y = -1.0:0.1:1.0; axes_handle = gca; movie_frames(1) = getframe(capture_figure_handle); movie_frames(2) = getframe(capture_figure_handle); % Optionally display the movie in a new figure movie(movie_figure_handle, movie_frames, movie_rerun_count, frames_per_second);

## Calling Java from MATLAB

The following tutorial deals with this subject in detail: Calling Java from MATLAB.

## Increasing the heap size for Java VM

To increase the heap size for Java VM, creare a new file names `java.opts` under `C:\Program Files\Matlab\R2006b\bin\win32\` (modify to reflect the location of your MATLAB installation; the terminal win32 is your system architecture), if the file doesn't already exist, and add the following line to it:

-Xmx268435456

Here 268435456 is the required maximum heap size in bytes. You may wish to use a different value to suit your needs.

- This page was last modified on 6 September 2008, at 10:50.
- This page has been accessed 16,783 times.