Skip to content

Commit

Permalink
Merge pull request #101 from Galvant/feature-matlab
Browse files Browse the repository at this point in the history
Initial MATLAB fn and example.
  • Loading branch information
scasagrande committed Mar 30, 2016
2 parents 645398c + b732a03 commit c1447ec
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
79 changes: 79 additions & 0 deletions matlab/matlab-example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loading Instruments from MATLAB #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"MATLAB 2016a supports calling into Python, such that we can open instruments and communicate with them from within MATLAB applications. This takes a little bit of work, however, due to bugs in MATLAB's Python interface. Here, we'll demonstrate using the ``open_instrument.m`` MATLAB function to open instruments from their URIs."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
" File build/bdist.linux-x86_64/egg/serial/serialposix.py, line 294, in open\n",
"\n",
" File build/bdist.linux-x86_64/egg/serial/serialutil.py, line 180, in __init__\n",
"\n",
" File build/bdist.linux-x86_64/egg/instruments/abstract_instruments/comm/serial_manager.py, line 64, in new_serial_connection\n",
"\n",
" File build/bdist.linux-x86_64/egg/instruments/abstract_instruments/instrument.py, line 438, in open_serial\n",
"\n",
" File build/bdist.linux-x86_64/egg/instruments/abstract_instruments/instrument.py, line 355, in open_from_uri\n",
"\n",
" File <string>, line 1, in <module>\n",
"Python Error: SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'\n",
"\n"
]
}
],
"source": [
"instrument = open_instrument('phasematrix.PhaseMatrixFSW0020', 'serial:/dev/ttyUSB0')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "MLegacy Kernel",
"language": "python",
"name": "kernel_mlegacy"
},
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-matlab",
"name": "matlab"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
30 changes: 30 additions & 0 deletions matlab/open_instrument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function instrument = open_instrument(name, uri)
% open_instrument Opens an instrument given its InstrumentKit URI.
%
% WARNING: this function can execute arbitrary Python code, so do *not*
% call for untrusted URIs.

% We need to use py.eval, since using py.* directly doesn't work for
% @classmethods. This presents two drawbacks: first, we need to manage
% the Python globals() dict directly, and second, we need to do string
% manipulation to make the line of source code to evaluate.

% To manage globals() ourselves, we need to make a new dict() that we will
% pass to py.eval.
namespace = py.dict();

% Next, py.eval doesn't respect MATLAB's import py.* command-form function.
% Thus, we need to use the __import__ built-in function to return the module
% object for InstrumentKit. We'll save it directly into our new namespace,
% so that it becomes a global for the next py.eval. Recall that d{'x'} on the
% MATLAB side corresponds to d['x'] on the Python side, for d a Python dict().
namespace{'ik'} = py.eval('__import__("instruments")', namespace);

% Finally, we're equipped to run the open_from_uri @classmethod. To do so,
% we want to evaluate a line that looks like:
% ik.holzworth.Holzworth.HS9000.open_from_uri(r"serial:/dev/ttyUSB0")
% We use r to cut down on accidental escaping errors, but importantly, this will
% do *nothing* to cut down intentional abuse of eval.
instrument = py.eval(['ik.' name '.open_from_uri(r"' uri '")'], namespace);

end

0 comments on commit c1447ec

Please sign in to comment.