Personal tools
User menu

Knowledge Base/MATLAB/General

From Thalesians

Jump to: navigation, search

Contents

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:

  1. s = 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 ...
  2. - 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:

>> import java.util.*;
>> map = Hashtable;
>> map.put('paul', 24);
>> map.get('paul')
 
ans =
 
    24

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...

  1. x = -1.0:0.1:1.0;
  2. y = -1.0:0.1:1.0;
  3.  
  4. figure_handle = figure();
  5.  
  6. axes_handle = gca;
  7.  
  8. hold on
  9. plot_handles(1) = plot(axes_handle, x, y);
  10. plot_handles(2) = plot(axes_handle, x, -y);

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

  1. figure_handle = figure();

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:

  1. x = -1.0:0.1:1.0;
  2. y = -1.0:0.1:1.0;
  3.  
  4. figure_handle = figure('Name', 'Regimes of Volatility', 'Visible', 'Off');
  5.  
  6. axes_handle = gca;
  7.  
  8. hold on
  9. plot_handles(1) = plot(axes_handle, x, y);
  10. plot_handles(2) = plot(axes_handle, x, -y);

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

  1. set(figure_handle, 'Visible', 'On')

and

  1. set(figure_handle, 'Visible', 'Off')

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.

  1. movie_rerun_count = 3;
  2. frames_per_second = 2;
  3.  
  4. x = -1.0:0.1:1.0;
  5. y = -1.0:0.1:1.0;
  6.  
  7. capture_figure_handle = figure('Name', 'Regimes of Volatility');
  8.  
  9. axes_handle = gca;
  10. set(axes_handle, 'NextPlot', 'replacechildren');
  11. title(axes_handle, 'Regimes of Volatility');
  12. xlabel(axes_handle, 'x');
  13.  
  14. plot_handles(1) = plot(axes_handle, x, y);
  15. movie_frames(1) = getframe(capture_figure_handle);
  16.  
  17. plot_handles(2) = plot(axes_handle, x, -y);
  18. movie_frames(2) = getframe(capture_figure_handle);
  19.  
  20. mpgwrite(movie_frames, colormap, 'C:\test.mpg');
  21.  
  22. % Optionally display the movie in a new figure
  23. movie_figure_handle = figure('Name', 'Regimes of Volatility - Animation');
  24. 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.