Posts tagged with MATLAB

0 comments

Competition

Like many of you, I went to monster truck show last weekend.

The competition reminded me MATLAB’s new thing: Cody.

It’s a bunch of problems posed as MATLAB coding tasks. For example:

Find the mean of each consecutive pair of numbers in the input row vector. For example,

x=[1 2 3] —-> y = [1.5 2.5]

x=[100 0 0 0 100] —-> y = [50 0 0 50]

To solve the problem, you write a MATLAB program that performs that input-output transformation. Of the correct answers, the shortest program wins. Answers are automatically checked for correctness and there is a graphic display of length and correctness of submitted answers:

At the very least, I think this is a clever tool for learning MATLAB. Working through exercises is pretty dry. This is effectively the same thing, but the competetion aspect is energizing. Most problems are locked until you solve easier ones. You can submit your own problems too. So this could be a sneaky way to crowd source your own work.

Long live competion.

In two previous posts I shared some MATLAB code to help design collection optics in 2p scopes.
Collection optics for 2p scopes, post 1
Collection optics for 2p scopes, post 2
It was just brought to my attention that I didn’t include the command locateVal in the code I posted. It’s a very simple little shortcut I use. Here it is:

function [pos difference] = locateVal(val,data)
[difference pos] = min(abs(data - val));


Yes, you could have guessed that. But I wanted to correct the oversight.

On the topic of iOS apps, MATLAB has released v3.0 of their mobile app. The most apparent changes are UI usability improvements.
(MATLAB link, App Store link)

On the topic of MATLAB learning materials (covered previously here and lots more MATLAB stuff here), MIT has some online courses freely available. Here’s an “aggressively gentle” intro to MATLAB, and some more MATLAB resources. (Hat tip to MH)

Also here’s a link from an older post on xcorr (Patrick Mineault’s excellent blog). This course webpage has a bunch of examples in MATLAB code. They’re great for simultaneously learning MATLAB and visual neuroscience.

ScanImage is an excellent software package for controlling 2p scopes. It’s free and open source. It’s been actively developed and released to the public since its inception. RIght now they personnel involved are trying to renew their funding. To help keep this resource actively developed and free, please fill out their survey. It’s very, very short. Don’t take the resource for granted. It takes a lot of salaried time to keep the development going and adding in new features.

By the way, ScanImage 3.8 (new features) and 4.0 (for ThorLabs scopes) are out now (3.5 and 3.6 are no longer supported; 3.7.1 is the current stable release) (link). If you haven’t already tried a new version of ScanImage out, you should. It doesn’t take too long and the feedback is very helpful. Don’t assume that everyone else is already sending in the same feedback.

A colleague once told me: “in MATLAB, drawing a raster plot is a trivial, one line command”. True, but then you spend another 10 lines of code trying to make it not look like complete crap. Which is both time consuming and futile. In the end, you know you’re going to be spending half a day with Adobe Illustrator to fix it. But it does all start with one line of code.

Here’s some code to draw simple, one line raster plots:

function raster(in)
if size(in,1) > size(in,2)
    in=in';
end
axis([0 max(in)+1 -1 2])
plot([in;in],[ones(size(in));zeros(size(in))],'k-')
set(gca,'TickDir','out') % draw the tick marks on the outside
set(gca,'YTick', []) % don't draw y-axis ticks
set(gca,'PlotBoxAspectRatio',[1 0.05 1]) % short and wide
set(gca,'Color',get(gcf,'Color')) % match figure background
set(gca,'YColor',get(gcf,'Color')) % hide the y axis
box off

As an aside…

Looking for a wry take on MATLAB’s shortcomings from an anonymous neuroscientist? Try Abandon MATLAB. This blog’s posts include these gems:
Matlab doesn’t know how to draw one ball out of an urn containing one ball
The Mathworks don’t even know how to look up functions in their own global namespace
And this image (source):

In many rigs, multiple computers are used, and they need to talk to each other. For example, one computer handles the imaging, another the electrophysiology, and yet another handles the sensory stimulation; and they all need to be synchronized. There are several ways to handle this.

Digital I/O via DAQs

This is typically the most straightforward solution, but requires that there is some sort of DAQ for each computer. You can use something very simple, like a parallel port interface. Parallel ports are very old fashioned, but they’re still standard issue on most full-sized motherboards (see image above). Wire up the each data pins to a separate BNC and you have an extremely inexpensive digital I/O (the pinout is on Wikipedia and elsewhere on the net). The logic is a TTL-like 0-5v, however, beware that motherboards will vary quite a bit in their logic-high voltage level, and they typically can only source a small amount of current, 10-20 mA. So don’t try to split the signal to a bunch of different devices. (more info).

Alternatively, you can buy an inexpensive USB DAQ. For example, Measurement Computing‘s boxes are supported by MATLAB’s DAQ Toolbox. On that topic, let me briefly mention Dennis Pelli’s excellent MATLAB toolbox for controlling Measurement Computing’s USB-1208FS. It’s one of the simplest ways to do data acquisition in OSX using MATLAB. And be sure to read his explanation about why he programmed it up. It has more to do with the Second Amendment than you might expect. (link)

Ethernet connections

This approach has the advantage of using something that most computers have built in: an ethernet port. There are several protocols that ride on IP networks. You’re probably most familiar with TCP, but only slightly less well known is this: UDP. This is the protocol used for DNS queries. This is a leaner, faster way to send short messages to synchronize computers in an experimental setup. It’s not as tight as using a direct pulse to a DAQ, but it’s fast enough for most applications. The big advantage is that it’s pretty simple to send configuration data in addition to a simple trigger pulse via UDP. Several scientific software packages directly support UDP (e.g., here’s MATLAB’s UDP function).

Single computer solution

Getting programs to talk to each other
Even when a single computer is used, there may be multiple programs that need to talk to each other. In the first intrinsic imaging rig I built, I wrote my own OpenGL-based visual stimulation program which ran on the same computer as the imaging software (pdf). Both programs were written in C++ using Visual Studio. In order to get the two computers to talk to each other, I used custom Windows messages. Most modern operating systems have a messaging system where individual processes can send and receive messages. I was able to use this to pass messages between the two programs. If you want to do the same thing, and are already comfortable dealing with Windows messages in C++, this link can get you started.

MATLAB version R2011b has just been released. The new format for error messages is not as mind blowing as you might have heard. However, there’s one cute little function that I’ve used already: automated variable renaming.

  • Maybe we’re sharing some code and want to use more descriptive and readable variable names than “x1″ and “x2″.
  • Or perhaps we’re reusing code and want to adhere to a different variable naming convention.
  • Or maybe we just picked a stupid variable name when we were whipping something up late at night (I once used “HK” to stand for “high chloride”… it made sense at the time, maybe you can guess why, but it was not a wise choice).
  • In all these cases, this new function is quite useful. A nice addition.

    MATLAB Mobile is a way to use MATLAB from an iOS device. It’s cute, but a bit odd, because you used to need to have MATLAB running on your own server in order to take advantage of it. With the newly released version 2.0, you now have the option of connecting to one of MathWorks’ servers, though you’ll still need a current MATLAB license.

    The command line looks familiar, and figure display works fine, but figure interaction and GUIs are not supported. They’re working on a way to upload your own files to the cloud server, but that’s not implemented just yet. Still, it’s an interesting tool.

    I already use MATLAB via Remote Desktop about 99% of the time that I’m doing analysis. The interaction is not as snappy as sitting right at the computer, but it’s an acceptable price to pay to be able to use the same hardware wherever I am. I’m curious to know whether MathWorks might extend this service in the future. I might pay to have access to their servers if they’re well maintained and can crunch data faster than I can on my own computers.

    More science apps

    There are several MATLAB-related items to post, but individually they don’t amount to much, so I’ll just put them all together.

    Outside of MATLAB Central, there are a lot of places to find useful MATLAB code and commentary. In particular, some labs have excellently curated MATLAB toolboxes and are willing to share with their friends. So ask around. No need to reinvent the wheel.

    As far as what’s publicly available, I’ve stumbled across a couple that have been handy. Blogger Stinkpot writes very brief notes on MATLAB sporadically. Items include Moving the X-axis to the top of a plot and Inserting a MATLAB matrix in to LaTeX.

    Peter Acklam offers a nicely organized list of MATLAB utilities. Although some of the functions are quite old and are now included in MATLAB natively (e.g. the Kronecker tensor product), others are fairly unique and may be useful to you. There are also somewhat obscure things like trigonometric functions using gradians (e.g. cosgrad).

    Xcorr has had a couple of excellent posts recently on some perhaps obscure MATLAB functions. There is an excellent explanation and demo for each function. Highly recommended. Xcorr’s coverage of memmapfile and bsxfun.

    Finally, you might be interested in GPU computing with MATLAB. AccelerEyes‘ Libjacket is finally 1.0 (link). This uses NVIDIA hardware to accelerate MATLAB computations. The graph at the top of this post compares the performance of their software running on an NVIDIA Tesla C2050 to an alternative, CPU-based acceleration technology (Intel’s Math Kernel Library). Academic research has found similar advantages (link).