## How to duplicate Matlab functionality

Since I am a total Matlab-n00b, I rather write a convoluted Python script instead of using the appropriate Matlab function. And here it is. A small script to compute the standard deviation of two vectors. I guess this would have been one Matlab command.

`import sys                                                                                                                                                                                       import scipy.io                                                                                                                                                                                  import math                                                                                                                                                                                                                                                                                                                                                                                      def computeStdDev(file, vec1, vec2):                                                                                                                                                                data = scipy.io.loadmat(file, struct_as_record=True)                                                                                                                                            vector1 = data[vec1]                                                                                                                                                                            vector2 = data[vec2]                                                                                                                                                                            if len(vector1) != len(vector2):                                                                                                                                                                    print "Error: Vectors do not have matching lengths."                                                                                                                                            return                                                                                                                                                                                      diff = vector1                                                                                                                                                                                  N = len(diff)                                                                                                                                                                                                                                                                                                                                                                                   for i in range(N):                                                                                                                                                                                  diff[i] = vector1[i] - vector2[i]                                                                                                                                                                                                                                                                                                                                                           mu = 0                                                                                                                                                                                          for val in diff:                                                                                                                                                                                    mu = mu + val                                                                                                                                                                               mu = mu / float(N)                                                                                                                                                                                                                                                                                                                                                                              sigma = 0                                                                                                                                                                                       for i in range(N):                                                                                                                                                                                  sigma = sigma + (diff[i] - mu)**2                                                                                                                                                           sigma = math.sqrt(sigma / float(N))                                                                                                                                                                                                                                                                                                                                                             print "mu: %f" % mu                                                                                                                                                                             print "sigma: %f" % sigma                                                                                                                                                                                                                                                                                                                                                                    if len(sys.argv) != 4:                                                                                                                                                                              print "Usage: %s file vector1 vector2" % sys.argv[0]                                                                                                                                            sys.exit(0)                                                                                                                                                                                                                                                                                                                                                                                  computeStdDev(sys.argv[1], sys.argv[2], sys.argv[3])                                                                                                                                             `

## Matlab and Python

How to read Matlab files with Python and SciPy can be read here. Short:

`import scipy.iodata = scipy.io.loadmat('test.mat')`

This will create a dictionary called data containing all the variables of the Matlab file.

## jabber.org is coughing

The guys over at jabber.org are moving servers, and upgrading their software. However, it does not seem to work well. They mention the need to adjust settings for iChat and Kopete. However my IM+ does not work anymore either, Licq’s pre-alpha jabber Plugin also crashes (however erijo seems to be working on a fix), and Adium also chokes, and will connect only every couple of hours. It seems they should have done a bit more testing before!

## Mobile me, Mobile schmu

I am currently trying out Apple’s Mobile Me service, which is free in the first two months. It seems relatively nice, but has some drawbacks. On the plus side, you get 20 GB of storage space, or 40 GB for the family pack. Also you can track your iPhone if you lose it. The mail service is useful, but on the downside: It does not support server side filtering! That’s a great, great bummer. Also it does not seem to support Apple’s notion of filtering, called “intelligent mailboxes”. If that were supported on the server, I would ditch my GMX ProMail account, and switch to using Mobile Me for mail services.

## Elegantly making a dictionary of lists

In Python I sometimes want to make a dictionary of lists. So usually I would write something like:

`mydic = {}for something in somethingelse:    mydic[somekey].append(something)`

This however will not work, if somekey did not exist so far, because it will raise an exception. But there is help! You can use a collection:

`import collectionsmydic = collections.defaultdic(list)for something in somethingelse:    mydic[somekey].append(something)`

## PyQt dynamic uic example

Here is a minimal example for PyQt4, to set up a main window and connect a simple action to some slot. The documentation on doing this is sparse to say the least. So I hope this is helpful for anyone trying something similar. I assume that you have created a ui_MainWindow.ui file with Qt Designer, which contains a form derived from QMainWindow.

`import sysfrom PyQt4 import QtCore, QtGui, uicclass MyMainWindow(QtGui.QMainWindow):   def __init__(self):      QtGui.QMainWindow.__init__(self)      self.ui = uic.loadUi("ui_MainWindow.ui", self)      self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'), self, QtCore.SLOT('open()'))      @QtCore.pyqtSlot()   def open(self):      fileDialog = QtGui.QFileDialog(self, "Open file")      fileDialog.show()app = QtGui.QApplication(sys.argv)mainWindow = MyMainWindow()mainWindow.show()sys.exit(app.exec_())`

## Not happy with Qt on OS X…

I really don’t get happy with Qt 4.6 on OS X 10.6. Nokia rates Qt on that platform as Tier 2. Which means, it is not fully supported. This results in stupid things happening. With Licq, I currently have the problem that the whole program crashes with the following message:

+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.

The problem here is that Qt 4.6 for 64 Bit is (rightly so) built using the Cocoa API, instead of the C-only Carbon API. Now it seems that Qt uses the NSUndoManager, which in turn has an undocumented (?) function _endTopLevelGroupings. And as the above mentioned message tells you, it is only safe to call that function from the main thread. Since the qt4-gui plugin of Licq runs in a separate thread, this assertion fails, and the whole program crashes. So this basically means you cannot start up your GUI from a secondary thread with Cocoa based Qt. This, I think, constitutes a bug in Qt.
Update: I submitted a bug tracker item, and erijo is producing a minimal example that will hopefully show the bug in action. Now let’s wait and see.

## Preprocessor Tricks

This allows you to convert any preprocessor macro value to a string, e.g. for printing.

`#include <iostream>#define MEINMAKRO 1.3.4#define STRINGIFY(x) #x#define TOSTRING(x) STRINGIFY(x)int main(){  std::cout << TOSTRING(MEINMAKRO) << std::endl;  return 0;}`

## Syncing Google Calendar with the iPhone or iPod Touch

Since several people asked me how to do this, after I twittered about this, here it comes: How to correctly sync the Google Calendar with the iPhone. First step is to create a new calendar account in the iPhone settings (Email, Calendar, Contacts) using the Exchange protocol. The finished account should look something like this:
You can also sync the contacts and emails, if you want, but that’s optional. The second step is to go to http://m.google.com/sync/ to set up which of your google calendars you are going to sync. Beware: on the german iPhones, the page will display an error message, unless you switch to the english page!
That seems to be a bug on google’s page. So after you’ve done this, you get a list of your iPhones and can select for each device which calendars to sync: