Pages

Thursday, December 19, 2013

Passing Data From Matlab to Python and Back

The following python and Matlab scripts will:
    1) Pass two variables form Matlab to python,
    2) process the variables in python, and
    3) return the result back to Matlab

The idea is to use the Matlab system() function to call a pre-defined python script (multiplySctipt.py) and then use the python sys.stdout.write() method to send the result back to Matlab. The result is returned to Matlab as text.

Note that this method is fine for small amounts of data, however, passing large amounts of data can take a long time.

---------------------------------------------------------------------
# Simple Python Command Line Script to take 2 numbers as inputs and
# output the product of the two numbers.  I saved this file as
# '/MyDocuments/python/multiplyScript.py'.

import sys, getopt

def main(argv):
    try:
        myopts, args = getopt.getopt(sys.argv[1:],'x:y:')
    except getopt.getoptError:
        print(str(getopt.getoptError))
        print("Usage: %s -x <1st number> -y <2nd number>" % sys.argv[0])
        print("Example: %s -x 3.14 -y 2.0" % sys.argv[0])
        sys.exit(2)

    #######################
    # o == option
    # a == argument
    for o,a in myopts:
        if o == '-x':
            x = float(a)
        if o == '-y':
            y = float(a)
    sys.stdout.write(str(x*y))

if __name__ == "__main__":

    main(sys.argv[1:])
---------------------------------------------------------------------

---------------------------------------------------------------------
% Simple Matlab script to run python script and get output
pythonScript = ['/MyDocuments/python/multiplyScript.py'];
x = num2str(3.14);
y = num2str(2.0);
[status,cmdout] = system(['python ',pythonScript,' -x ',x,' -y ',y]);
cmdout = str2num(cmdout);
---------------------------------------------------------------------

Wednesday, December 4, 2013

How to Programmatically Format Command Window Text

References: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/
                    http://undocumentedmatlab.com/blog/bold-color-text-in-the-command-window/
                    http://undocumentedmatlab.com/blog/another-command-window-text-color-hack/


stringVar = 3.142; % some variable to use in the fprintf string function

% Red Text
fprintf(2, 'this is some text %1.3f', stringVar); % the 2 indicates an error message

% Blue Underlined Text (i.e. hyperlink format):
fprintf('this is some <a href="">blue underlined text %1.3f</a>', stringVar);

% Bold Text
fprintf('this is some <strong>bold text %1.3f</strong>', stringVar);

% Orange Text
fprintf('this is some [\borange text %1.3f]\b', stringVar);

How to Programatically Change the Background and Foreground Color of the Matlab Command Window

Reference: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/

Code:

import com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
listeners = cmdWinDoc.getDocmentListeners;
for ii = 1:length(listeners)
    if ~isempty(regexp(char(listeners(ii).toString()),'JTextArea'))
        jTextArea = listeners(ii);
        break;
    end
end
jTextArea.setBackground(java.awt.Color.yellow);
jTextArea.setBackground(java.awt.Color(1,1,0));
set(jTextArea,'Background',[1,1,0]);
set(jTextArea,'Foreground',[0,1,1]);