If you haven’t heard already OS X 10.8 (AKA Mountain Lion – ML) was released this week. For an in depth review I recommend John Siracusa’s review. One of the things that struck me, as I’ve been reading on the new features of ML, was that X11 is no longer included in OS X. Continue reading

# Category Archives: Matlab

# Temporary text expanding snippets

I actually got this idea from David Sparks in one of the Mac power users podcast episodes. Because I’ve been catching up with past episodes, while also listening to new ones I haven’t been able to remember in which episode exactly he mentioned this, but it’s been one *hell of a tip*.

On any given project, sometimes you might find yourself having to write the same line over and over again, which is what text expanding apps applications are for. Continue reading

# Displaying numbers in MATLAB

A colleague of mine asked me yesterday that he wanted to get more decimal places on some numerical computation he was carrying out in MATLAB. He was actually using the curve fitting toolbox to fit a custom function and obtain two parameters `a`

and `b`

, but he would get something like `a=1.345`

, and he wanted to know at least two or three more decimal places. Typically the numeric information for these data is sitting in double precision in the MATLAB variables, however you have to know how to change the default format how MATLAB displays numbers to get it in the numeric format you want.

This post in the blog Loren on the Art of MATLAB explains the details. The short explanation is to use the `format`

function. The default option is `short`

, so if you write `pi`

at the command line you get `ans = 3.1416`

, but if you the write `format long`

before calling `pi`

, this time you get `ans = 3.141592653589793`

. There are also other types of notation depending on what you need.

# Posting MATLAB Code on WordPress.com

Until recently whenever I wanted to post some code on my wordpress blog I used to put something like this

` ````
function m = magicsquare(n)
%MAGICSQUARE generates a magic square matrix of the size specified
% by the input parameter n.
% Copyright 2003-2006 The MathWorks, Inc.
if ischar(n)
n=str2num(n);
end
m = magic(n);
```

Which is actually not bad at all. But what if I wanted it with syntax highlights for a specific language, say C++, Java, or why not MATLAB as well. And on top of that, that you could get line numbering, line highlighting, no column wrapping, and a bunch of other cool ways to display code. Well the guys at wordpress came up with a nice way to do it:

We have created a shortcode you can wrap around source code that preserves its formatting and even provides syntax highlighting for certain languages […] To accomplish the above, just wrap your code in these tags:

[sourcecode language=”css”]

your code here

[/sourcecode]

via Posting Source Code — Support — WordPress.com.

So if I input something like this:

`[`

`sourcecode language="matlab"] function m = magicsquare(n) %MAGICSQUARE generates a magic square matrix of the size specified % by the input parameter n. % Copyright 2003-2006 The MathWorks, Inc. if ischar(n) n=str2num(n); end m = magic(n); [/sourcecode]`

I actually get this:

function m = magicsquare(n) %MAGICSQUARE generates a magic square matrix of the size specified % by the input parameter n. % Copyright 2003-2006 The MathWorks, Inc. if ischar(n) n=str2num(n); end m = magic(n);

Pretty neat! There are a lot of options for the shortcode check the link to see them.

# Search the Matlab history

If you ever need to search through the MATLAB command history you can do it in the following way.

To accomplish it in a programmatic and platform-independent manner, you can first use MATLAB’s Java internals to get the command history as a character array:

history = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; historyText = char(history);

Then you can search through the character array however you like, using functions like STRFIND or REGEXP. You can also turn the character array into a cell array of strings (one line per cell) with the function CELLSTR, since they can sometimes be easier to work with.

via How do I search through MATLAB command history? – Stack Overflow.

Changing the last line to `.getAllHistory`

should get you the whole history, whereas the previous one will get you just the history from the current session.

history = com.mathworks.mlservices.MLCommandHistoryServices.getAllHistory;

# Matlab in iPhone, iPad, and iPod Touch

I’ve recently found out about a lightweight desktop on your iOS device called MATLAB Mobile. It connects to a MATLAB session running on the MathWorks Cloud or on your computer. It let’s you run scripts, create figures, and view results. I’ve tried it on my iPod Touch and it works great. Haven’t been able to try it on the MathWorks Cloud, but at least in my computer it works great. It has some nice features like automatic parentheses matching and special keys in the extended keyboard so that it is easier to code from the iOS keyboard. You can zoom in a figure, resize it, and play with it just as if you were on your desktop. The fact is that the figure is “actually” doing the same thing in your desktop.

I think this is a nice complement for us that use MATLAB on a daily basis. Either for research or for teaching. It’s a nice way to have the power of MATLAB at your fingertips, without having to carry around a laptop. You can have your predefined scripts and libraries without having to depend on MATLAB being installed on other computers and carrying your scripts in an pendrive. This should become available as well for use on other computers like a special VNC client.

A final note: I’ve also found a similar app from the MATLAB like software GNU-Octave, it’s called Octave Remote. Apparently is does the same thing, however only on the Cloud. I’ve tried some basic things and it seems to work quite nice. At some point it suddenly closed, but this also happens with other apps so it’s not really a surprise. We’ll see how this continues to develop.