Wednesday, September 26, 2007

[Matlab] Use Plain Text in labels and legends

Source

Matlab Help. Search for keyword 'interpreter'.

Description

To print the labels and the legends in plain text such that `_' and `^' will not be transcribed to subscript and superscript specifier.

Syntax

% Interpreter_type: 'latex' | {'tex'} | 'none'
set(handle, 'Interpreter', Interpreter_type );

Code

h_label = xlabel('MY_LABEL');
set(h_label, 'Interpreter', 'none');

h_legend = legend('LEG_1', 'LEG_2');
set(h_legend, 'Interpreter', 'none');

Friday, June 29, 2007

[Matlab][Debug]mxDestroyArray

Source

Matlab Help: Memory Management Compatibility Issues

Syndrome

The matlab engine properly opens and closes at the first time. But at the second attempt of starting matlab engine the error message pops up:
Forced Assertion: "Corrupted memory block found"

Cause

  • mxDestroyArray was called on a mxArray whose data was not assigned by matlab API
    mxArray *temp = mxCreateDoubleMatrix(1,5,mxREAL);
    double data[5] = {1,2,3,4,5};
    ...
    memcpy(mxGetPr(temp), data, 5*sizeof(double));
    ...
    mxDestroyArray(temp); // <-- INCORRECT. Do NOT call mxDestroyArray on temp

Solution

  • Do NOT call mxDestroyArray on such mxArray's

Thursday, June 14, 2007

C++ and C FAQ

Source

  • Comeau C++ and C FAQ
  • Embedded System Design
  • C++ FAQ LITE

    Description

    You can probably find answers to the fundamental questions about c++ programming.

    Memo

    • Constant objects can be declared with various types and user can access the address of the constant objects.
    • Enumeration Constants can only be integers and the address of enumeration constant is not available. But it also means that compilers won't generate codes for the ability to take the address, which becomes an advantage of using enumeration constants if users do not need the address of the constant.
    • When writing class template, the definition and the declaration should "be seen by compiler at the same time," which means one may 1. write both the declaration and the implementation in the same file (the .h file); 2. include the implementation file (.cpp) in the header file (.h), not include the header file in the implmenetation file. To explain this in one sentence: A class template is not a real class and the compiler will not see the functions of the class template with specified type.

  • Friday, June 08, 2007

    [LINUX] Bash Programming

    Source

    BASH Programming - Introduction HOW-TO by Mike G
    Advanced Bash-Scripting Guide by Mendel Cooper
    Bourne Shell Programming

    [LINUX] If The Script Does Not Run

    Check:

    • Is the script saved in Linux/Unix format (If saved in DOS format you may see "Command not found" error message). The command to remove the '\r' from DOS format is
      $ tr -d '\r' < DOS_file > UNIX_file
      The '<' is to input the DOS_file to command 'tr' and '>' is to output the file without '\r'. The commands including UNIX-to-DOS conversion can be found here
    • Is the script executable (if not you may see "Permission denied" error message

    Thursday, May 31, 2007

    [Software] PSPad Issues

    PSPad changes the unix permission when editing files
    Forum discussion: http://forum.pspad.com/read.php?2,18205,18205#msg-18205
    Solution:
    Program settings --> Direct edit
    [x]DontChangeFileAttribs=1

    Wednesday, April 25, 2007

    [Perl] Regular Expression With Chinese Char

    Use \Q中文字\E

    Tuesday, April 24, 2007

    Windows Batch Commands

    Source

    http://www.computerhope.com/batch.htm
    Run .bat file in a .bat file

    Codes:

    call bat1.bat
    call bat2.bat

    Monday, March 05, 2007

    [Matlab] Plot lines with different markers

    Source

    Matlab Help:
    • Find "LineSpec" to see the available markers

    Codes

    % Initialize a set of desired markers
    MarkerSet = 'x*o+s.^v';
    plot( x, y ); grid on;
    % Obtain all line handles under current axes handle
    hdl = findobj(gca,'Type','line');
    for k=1:length(hdl)
    % Assign markers to each line handle
    set(hdl(k), 'marker', MarkerSet(k) );
    end

    [Matlab] Plotyy and the Labeling

    Source:

    Matlab Help

    Codes:

    MY_XLABEL = 'This is xlabel';
    MY_Y1LABEL = 'This is y1 label';
    MY_Y2LABEL = 'This is y2 label';
    MY_Y1LEGEND = {'y1_lg1', 'y1_lg2' };
    MY_Y2LEGEND = {'y2_lg1', 'y2_lg2' };
    MY_TITLE = 'This is my title';

    [AX,H1,H2] = plotyy(x1, y1, x2, y2);
    xlabel( MY_XLABEL );
    set(get(AX(1),'Ylabel'),'String', MY_Y1LABEL );
    set(get(AX(2),'Ylabel'),'String', MY_Y2LABEL );
    legend(H1, MY_Y1LEGEND, 'Location', 'NorthWest' );
    legend(H2, MY_Y2LEGEND, 'Location', 'NorthEast' );
    title( MY_TITLE );

    Tuesday, February 20, 2007

    [HTML] Symbol Reference

    Greek, mathematical, symbolic characters, and so on.
    http://everything2.com/index.pl?node=HTML%20symbol%20reference

    Wednesday, January 17, 2007

    [Matlab] Data Clipping, saturation

    % 'x > upperbound' evaluates to a logical index
    x = array to be clipped;
    x( x > upperbound ) = upperbound;
    x( x < lowerbound ) = lowerbound;

    Tuesday, January 02, 2007

    Quotes

    "There is only one thing more beautiful than one guitar - Two guitars" Frederic Chopin

    Wednesday, November 15, 2006

    [EE] Forum and Sites

    DSP related:
    http://www.dsprelated.com/

    Monday, November 06, 2006

    [Math] Statistics Online Resources

    http://davidmlane.com/hyperstat/index.html

    Friday, November 03, 2006

    [HTML] Tags

    Link:

    Bare Bones Guide to HTML

    Frequently Used Tags

    4.0* Underline <U></U> (not widely implemented)

    Monday, October 02, 2006

    [Matlab][Cpp] Time Telling

    [Matlab] Generate file name with time

    Codes:

    currTime = clock;
    fname = sprintf('myfilename_%02d%02d%02d%02d.txt', currTime(2:5) );

    [Matlab]Timer
    Measure how many seconds were elapsed.

    Source:

    Matlab Help

    Codes:

    t1 = clock;
    %%%%%%%%%%%%%%%%
    % Run your scripts
    %%%%%%%%%%%%%%%%
    t2 = clock;
    Elapsed = etime(t2,t1);

    [CPP] Timer Code
    Measure how many seconds were elapsed.

    Source:

    http://www.cplusplus.com/ref/ctime/time.html

    Codes:

    #include <time.h>
    time_t stime, etime;
    stime = time(NULL);
    ...<Codes to be timed.>...
    etime = time(NULL);
    printf("Time elapsed: %ld seconds\n", etime-stime);

    [CPP] Wait function
    Make the program wait for several seconds.

    Source:

    http://www.cplusplus.com/ref/ctime/clock.html

    Codes:

    #include <time.h>

    int seconds = 10;
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}

    [CPP] Print time/date info
    Print the current date and time

    Source:

    http://www.cplusplus.com/reference/clibrary/ctime/ctime.html

    Codes:

    #include <stdio.h>
    #include <time.h>

    int main ()
    {
    time_t rawtime;

    time ( &rawtime );
    printf ( "The current local time is: %s", ctime (&rawtime) );

    return 0;
    }

    Monday, August 28, 2006

    [Tools] Site Links

    Equation Sheet
    A website containing many frequently used equations including following fields:
    Algebra
    Calculus
    Chemistry
    Geometry
    Physics
    Signal Processing
    Statistics
    Trigonometry

    Thursday, August 24, 2006

    [Matlab][CPP] Execute Command Line Commands

    [Matlab] Execute Command Line Commands

    Source

    Matlab Helps

    Syntax:

    [status, result] = system('command')
    [CPP] Execute Command Line Commands

    Source

    Microsoft MSDN

    Syntax:

    int result = system( const char *command );

    Codes:

    char cmd[200];
    sprintf( cmd, "copy output.txt output_%d_%d_%d.txt", i, j, k);
    system( cmd );

    [Matlab] Figure Commands

    Save Figure

    Source:

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/saveas.html
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/print.html

    Syntax:

    Method 1:

    saveas(h,'filename.ext')
    saveas(h,'filename','format')

    Method 2: (Using print function which allows user-defined print size)

    set(h, 'PaperPositionMode', 'auto' );
    set(h, 'PaperUnits', 'points');
    set(h, 'PaperSize', [width height] );
    print -dformat -rresolution filename;

    Example:

    Method 1:

    h = figure;
    plot(sin(0:0.1/2*pi:2*pi));
    saveas(h, 'd:\temp\test.jpg'); %Automatically save it as a jpeg file
    saveas(h, 'd:\temp\test.dat', 'fig'); % Force matlab to save it as a fig file

    Method 2:

    h = figure('Position', [1 100 900 500] );
    plot(sin(0:0.1/2*pi:2*pi));

    % Set 'PaperPositionMode' to 'auto' so that the print function will not resize the figure
    set(h, 'PaperPositionMode', 'auto' );
    % Set 'PaperUnits' to 'points' so that the the paper size can be defined in points.
    set(h, 'PaperUnits', 'points');
    % Set 'PaperSize' to the desired size in points. For some reason the value of width and height will be 4/3 times in pixels in the output file when printing the file at screen resolution (using -r0).
    set(h, 'PaperSize', [900 500]*3/4 );
    % use '-djpeg' to print the file in jpeg format. Use -r0 to specify the print resolution equal to screen resolution.
    print -djpeg -r0 'test.jpg'

    Support formats:

    ai: Adobe Illustrator `88
    bmp: Windows bitmap
    emf: Enhanced metafile
    eps: EPS Level 1
    fig: MATLAB figure (invalid for Simulink models)
    jpg: JPEG image (invalid for Simulink models)
    m: MATLAB M-file (invalid for Simulink models)
    pbm: Portable bitmap
    pcx: Paintbrush 24-bit
    pgm: Portable Graymap
    png: Portable Network Graphic
    sppm: Portable Pixmap
    tif: TIFF image, compressed

    Adjust the plot range

    Source:

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/index.html?/access/helpdesk/help/techdoc/ref/xlim.html

    Syntax

    xlim([xmin xmax]);