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.
iS = InputSource();
xml = xmlread(iS);
Some useful DOM methods:
- Get a list of nodes (tags):
nodes = xml.getElementsByTagName(nodename);
nodename is what stands after < and after </
photos = xml.getElementsByTagName('photos');
- Get the number of nodes found:
The first index is 0.
- Get node attribute value like id in <user id=”12037949754@N01″>:
- Get node value of a text-node like <username>Bees</username>:
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/.
- Callback value for stand-alone applications: oob, mentioned here.
- 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
- 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.
- 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.
- 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
- XML response received from Flickr may be read using Matlab XmlDOM functionality. I have a short separate post on it.
- Alternatively, one can obtain the response in the JSON format, using two additional parameters:
nojsoncallback = 1.
addpath adds a folder to the Matlab search path. It will again be forgotten after Matlab is restarted. To ‘forget’ it without restarting Matlab, first execute
path to list all the folders in the path, then copy the folder (full path) you want Matlab to forget, and do
If a few curves are plotted in the same axes, the one plotted first may get obscured by those plotted later. To move it to the front (or top), there is a command:
h is the curve object handle. Other options are
'bottom'. See http://www.mathworks.de/help/techdoc/ref/uistack.html for details.
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;
1x2 struct array with fields:
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
Sometimes (e. g. when using plot2svg to get figures from Matlab to Inkscape), the text objects in Inkscape do not react to changing the text size.
Check if the selected objects are actually text (select and watch the status line). If they are actually groups, un-group them further (Ctrl+Shift+G) and then change the font size.
- Download Matlab without JRE.
- Unzip MATLAB_R*_installer.zip, e.g.:
- Do not activate, you’re in superuser mode!
- 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)
- Matlab can be run from the terminal:
matlabor from GNOME (Alt+F2):
setsid matlab -desktop
- 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 here:
sudo 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).