Category Archives: Matlab

Processing XML data in Matlab

Many web-services return information in the form of an XML-formatted string. To process it, one can first convert this string into an XmlDOM object and then use its methods to get the data.

Converting XML string into object in Matlab.

import org.xml.sax.InputSource
import javax.xml.parsers.*
import java.io.*
iS = InputSource();
iS.setCharacterStream(StringReader(string));
xml = xmlread(iS);

Some useful DOM methods:

  • Get a list of nodes (tags): nodes = xml.getElementsByTagName(nodename);
    nodename is what stands after < and after </
    E.g. photos = xml.getElementsByTagName('photos');
  • Get the number of nodes found: nodes.getLength
    The first index is 0.
  • Get node attribute value like id in <user id=”12037949754@N01″>: nodes.item(0).getAttribute(attrName);
    E.g. char(photos.item(i).getAttribute('total'))
  • Get node value of a text-node like <username>Bees</username>: char(nodes.item(0).getTextContent)

Flickr photo hosting service provides an extensive API allowing for full control over the image files, tags, contacts, notes etc. Language-specific API’s are available for many programming languages (see http://www.flickr.com/services/api/). In a small tutorial (http://www.mathworks.com/matlabcentral/fileexchange/34162) I have shown how to use Flickr API in Matlab.

There are a few tricks to use when developing an app using Flickr API. They are not nicely described by Flickr (as of December 2011) and it takes some effort to find out how to do some simple things. Here I will be collecting the solutions.

I’d like to thank Sam Judson’s for help on the way to coping with Flickr’s OAuth. More information on Flickr OAuth can be found in his tutorial: http://www.wackylabs.net/2011/12/oauth-and-flickr-part-1/.


  1. Callback value for stand-alone applications: oob, mentioned here.
  2. When using urlread function of Matlab, in case something did not work as expected, Matlab shows a rather useless error message “Error using urlread (line 113). Error downloading URL. Your network connection may be down or your proxy settings improperly configured.” This is actually not at all what happens. In order to see what actually happens, paste the request line generated by the code (passed to urlread function) to the browser. Flickr will give a response explaining what it does not like, e.g.
    oauth_problem=timestamp_refused (check that your time is the Unix epoch time, in seconds, and that it’s UTC, not your local time),
    oauth_problem=nonce_used (each request to Flickr should have a unique random string identifier), or oauth_problem=signature_invalid&debug_sbs=GET&http%3A%2F%2Fwww.flickr.com...
  3. If you get the signature invalid error, it means that your request is generally correct but the signature is wrong. This is a tough problem since any tiny error in the base string algorithm will completely spoil the signature. To help resolving this issue, Flickr provides its own version of the base string, which you can compare to yours. I usually compare them in a Notepad by pasting the two strings one below another. The rules of building the base string are explained by Flickr and also in my tutorial.
  4. OAuth signature looks like a random string so it’s hard to debug. Therefore before searching for other problems in your code, make sure that your signature generation algorithm works correctly. You can compare it to this online signature generator: http://hash.online-convert.com/sha1-generator.
  5. Base string to be signed should be url-safe. Matlab provides function urlencode. Beware: it should be followed by
    s = strrep(s, '+', '%20')
    because of two versions of such encoding. See http://www.albionresearch.com/misc/urlencode.php
  6. XML response received from Flickr may be read using Matlab XmlDOM functionality. I have a short separate post on it.
  7. Alternatively, one can obtain the response in the JSON format, using two additional parameters: format=json and nojsoncallback = 1.

Matlab: How to access field values of a struct array

Problem: I have a structure s with the field f. I make an array of such structures:
>> s(1).f = 1;
>> s(2).f = 2;
>> s
s =
1x2 struct array with fields:
f
>> s.f
ans =
1
ans =
2

As can be seen, the answer is not a single array but rather two separate variables. As a result, this cannot be passed to other function that require an array argument, e.g. plot.

Solution: To create a variable containing the values s(:).f, just write [s.f]:
>> [s.f]
ans =
1    2

Install Matlab in Ubuntu

  1. Download Matlab without JRE.
  2. Unzip MATLAB_R*_installer.zip, e.g.: unzip matlab_R2011a_glnxa64_installer.zip
  3. Run sudo ./install.
  4. Do not activate, you’re in superuser mode!
  5. If the link is not created (on 2011-05-16 it didn’t create it though, according to this, it should have), create it yourself: sudo ln -s /usr/local/MATLAB/R2011a/bin/matlab /usr/local/bin/matlab (correct for your Matlab version)
  6. Matlab can be run from the terminal: matlab or from GNOME (Alt+F2): setsid matlab -desktop.
  7. If you like, get icon: sudo wget http://upload.wikimedia.org/wikipedia/commons/2/21/Matlab_Logo.png -O /usr/share/icons/matlab.png. An SVG icon is available heresudo wget http://6052500606069965693-a-1802744773732722657-s-sites.googlegroups.com/site/maltekoester/matlab_logo.svg -O /usr/share/icons/matlab.svg. This icon is used, e.g. in the Run Application dialogue (Alt+F2).