This little code snippet converts MATLAB Struct to xml, enjoy!!

%Convert Struct to XML

function obj = struct2xml(param)
   doc = com.mathworks.xml.XMLUtils.createDocument(inputname(1));
   root = doc.getDocumentElement;

   obj = xmlwrite(parse(param, doc, root));

function obj = parse(param, doc, root)
   if isstruct(param)
      fields = fieldnames(param);
      for i = 1 : numel(fields)
         if isstruct(param.(fields{i}))
            write(doc, root, fields{i}, '')
            parse(param.(fields{i}), doc, root.getLastChild)
            write(doc, root, fields{i}, param.(fields{i}))
     doc = 'could not convert';
   obj = doc;

function write(doc, root, pname, param)
   if isempty(param) == 1
      elem = doc.createElement(pname);
      if ismatrix(param)
         val = mat2str(param);
         val = num2str(param);

Mapping A Maze Using MATLAB Image Processing Toolbox

Recently I saw an article about mapping a maze and I thought to myself; how could this be achieved using my favorite tool MATLAB :-)? Well it turns out that its a pretty simple task. Some functions used in this post require that you have MATLAB 2012a or higher installed, so if u do not then go away and get it.

Wow you are back, ok so lets begin, first we will look at the code that was used and then further down we will look at individual functions and what they were used to do.

The maze:

The code:

%% Map Maze
im = imread('maze.png');
bw = im2bw(im(1:287, 1:400), 0.45);
cc = bwconncomp(bw, 8);
obj = false(size(bw));
obj(cc.PixelIdxList{13}) = true;
sln = bwmorph(bwmorph(obj,'thin',Inf),'spur', Inf);
figure, imshow(imfuse(im,sln,'blend','Scaling','joint'))

clear Clears all variables from the MATLAB workspace.
im = imread(‘maze.png’);
Reads the maze image and stores it.
bw = im2bw(im(1:287, 1:400), 0.45); Converts the image to binary, scales it and stores it.
cc = bwconncomp(bw, 8); Find connecting components within the image.
obj(cc.PixelIdxList{13}) = true; Find the largest component and select it.
sln = bwmorph(bwmorph(obj,’thin’,Inf),’spur’,Inf); Use Morphological functions to refine image.
figure, imshow(imfuse(im,sln,’blend’,’Scaling’,’joint’)); Overlay and display images


solved maze

The end :-)

Wheatstone Bridge In MATLAB Simulink

The diagram below describes a Wheatstone bridge designed with MATLAB Simulink, The resistor designated [R x] can be replaced with a variable resistance material and used in the construction of a strain gauge.

Wheatstone bridge is an electrical circuit used to measure an unknown electrical resistance by balancing two legs of a bridge circuit, one leg of which includes the unknown component. Its operation is similar to the original potentiometer. It was invented by Samuel Hunter Christie in 1833 and improved and popularized by Sir Charles Wheatstone in 1843. One of the Wheatstone bridge’s initial uses was for the purpose of soils analysis and comparison read more

Simulink Circuit Schematic:
wheatstone bridge

DC Voltage = 100 v
R 1 = 10 ohm, R 2 = 10 ohm, R 3 = 10 ohm, R x = 5 ohm
Connect the components as seen in the diagram then run the simulation, let [R x] be the variable resistance material; vary the resistance to see the difference in current.

NB: Resistors were taken from the  powerlib Simulink components [Parallel RLC Branch]

Solving Crossword Puzzles Using Image Acquisition And Data Scraping (Part 2)

In this the second part of the crossword puzzle solver series, we will look at writing to the video input stream. The image acquisition toolbox allows the programmer to configure a function called the UpdatePreviewWindowFcn . Once configured, we can use this function along with others from the vision toolbox like vision.TextInserter and vision.ShapeInserter to add artifacts to the video stream before calling the preview function.

Lets see some code:

vid = videoinput('winvideo', 1, 'YUY2_1280x720');
src = getselectedsource(vid);

vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';

vidRes   = get(vid, 'VideoResolution');
imWidth  = vidRes(1);
imHeight = vidRes(2);
nBands   = get(vid, 'NumberOfBands');
hImage   = image(zeros(imHeight, imWidth, nBands));

setappdata(hImage,'UpdatePreviewWindowFcn',@mypreview_fcn); % configure update udf.

preview(vid, hImage) % display updated stream.

Update Preview UDF code:

function mypreview_fcn(obj,event,himage)
    % display image data.
    tx = vision.TextInserter('Hello World!');
    tx.FontSize = 40;

    set(himage, 'cdata', step(tx, event.Data))

Result: See Hello World! printed in the top left hand corner of the stream.

This functionality will be very important when we get to actually superimposing the solution to the crossword puzzle on the video output stream.

Solving Crossword Puzzles Using Image Acquisition And Data Scraping (Part 1)

Crossword puzzles are a very popular pastime in most parts of the western world, and provide a quick means of escape for those on the commute or on break from regular work. The solution to the more interesting puzzles require a fairly good vocabulary and the ability to spot patterns and word arrangements. I recently saw an article on using MATLAB to solve a Sudoku puzzle using image acquisition toolbox and a Sudoku solver algorithm. This series of articles will take a slightly different approach, we will still use the image acquisition toolbox; however, instead of an algorithm to solve the puzzle we will use data scraping form a puzzle website to get the most suitable word for a particular place in the crossword.

The website we will be using to get solutions is  One Across

MATLAB is built on the java platform and is able to execute java code written at the command line or inserted in .m files. This feature will be exploited in future articles for doing tasks more suitable to the java toolset.

Example Java and MATLAB code in one .m file

import java.net.*;
import java.io.*;

data = URLEncoder.encode('http://www.oneacross.com/cgi-bin/search_banner.cgi?c0=&p0=r%3Foogle&s=+Go%21+');
url  = URL('http://www.oneacross.com/');
con  = url.openConnection(); con.setDoOutput(true);
wr   = OutputStreamWriter(con.getOutputStream());

rd   = BufferedReader(InputStreamReader(con.getInputStream()));

while rd.readLine() ~= '' %MATLAB CODE
    line = ans;

In part two of this series we will look at using image acquisition to capture a live video feed for the puzzle we want to solve and overlaying the possible solution unto the video feed, stay tuned.


Designing A Custom Neural Network In MATLAB

The MATLAB Neural Network toolbox ships with numerous predefined and canonical neural nets, however sometimes you may need to create a custom net with just the right connections, biases and hidden layers to suite your particular problem domain. To achieve this goal we can use the matlab network object. The  network object allows granular design of neural networks by exposing all properties of the net that we are designing. The preceding code demonstrates how to build a simple neural to learn the truth table for Logical AND.

First lets look at the Logical AND truth table:

p q p ∧ q

Open a new edit window in MATLAB and enter the following code:

    1. This creates an empty network object and assigns it to the net variable, sets up the number of inputs and uses cell array syntax to index into its properties.
      %% Design Custom Neural Network
      net = network;                                  % create network
      net.numInputs = 2;                              % set number of inputs
      net.inputs{1}.size = 1;                         % assign 2 to input size
      net.inputs{2}.size = 1;
      net.numLayers = 1;                              % add 1 layer to network
      net.layers{1}.size = 1;                         % assign number of neurons in layer
      net.inputConnect(1) = 1;                        % connet input to layer 1
      net.inputConnect(2) = 1;
      net.biasConnect(1) = 1;                         % connect bias to layer 1
      net.biases{1}.learnFcn = 'learnp';              % set bias learning function
      net.biases{1}.initFcn = 'initzero';             % set bias init function
      net.outputConnect(1) = 1;
      net.layers{1}.transferFcn = 'hardlim';          % set layer transfer function [hard limit]
      net.inputWeights{1}.initFcn = 'initzero';       % set input wieghts init function
      net.inputWeights{1}.learnFcn = 'learnp';        % set input weight learning function
      net.inputWeights{2}.learnFcn = 'learnp';
      net.inputWeights{2}.initFcn = 'initzero';
      net.initFcn = 'initlay';                        % set network init function
      net.trainFcn = 'trainc';                        % set network training function
      net.performFcn = 'mae';                         % set network perf evaluation function
      net = train(net,[0 0 1 1;0 1 0 1],[0 0 0 1]) ;  % train network
    2. Custom Network Diagram:
    3. Test Network
      In the command window type

      This should output a 1 to the command window indicating 1 AND 1 = 1


Image Classification Using MATLAB SOM/LVQ

I like to think of myself as a hacker :-), not in today’s sense of the word [person who breaks into secured computer areas] but as a hacker in the sense of first two definitions found here. I like to experiment with things especially related to computers and Artificial Intelligence in particular. MATLAB happens to be a virtual toolbox for the trade pun intended  :-), using some of its toolboxes we will see how we can solve a ubiquitous problem that faces most persons with a nice camera and a voracious appetite for taking pictures.

Now, I don’t have an expensive Nikon, but I do have loads of pictures; and one day i was trying to find this particular picture when it occurred to me that if i could not remember the name or date when i took the picture  it would require me to search blindly every picture I had in order to find that special one. Now what if i had some way of finding the picture based on what i could remember of it? ie. environment in which it was taken, colours and objects along with some other visual specifications, wouldn’t that be cool.

So I went for the proverbial toolbox MATLAB, which tools will I need?

  1. Neural Network    -> selforgmap, lvqnet, vec2ind, ind2vec
  2. Image Processing -> imhist, imresize, rgb2gray, imread


  1. mspaint

Note: For this demonstration I will be using:

  • MATLAB R2011b on Windows 7
  • Pictures found at  C:\Users\Public\Pictures\Sample Pictures

Ok lets do it,  start up MATLAB, copy and paste all the pics from the above directory to MATLAB’s current directory. {Chrysanthemum, Desert, Hydrangeas, Jellyfish, Koala, Lighthouse, Penguins, Tulips}.

  1. In the new edit window copy and paste the code as given below. Save file as scan.m
    function scan(img)
    files = dir('*.jpg');
    hist = [];
    for n = 1 : length(files)
        filename = files(n).name;
        file = imread(filename);
        hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; %#ok
    som = selforgmap([10 10]);
    som = train(som, hist);
    t   = som(hist); %extract class data
    net = lvqnet(10);
    net = train(net, hist, t);
    like(img, hist, files, net)

    Links to the functions used were provided above therefore i will not be going into the details of how they work, however there is a narrative with regard to the workings of the code:

    The code starts by searching the current MATLAB directory for all files with a .jpg extension. On each iteration of the loop an image is loaded and resized to 50 x 50, it is then converted to greyscale and a histogram measurement is taken of its pixels [feature vector]; the results are then appended to a 256 x n matrix with n been the number of images scanned.

    A self organizing map network is then used to identify classes into which the images fall. The feature matrix and class data is used to train a Learning Vector Quantization neural network, that will be used for classification of images presented to it.

  2. Next we will create a function to display all matching images for an image we submit to the LVQ network.
    function like(im, hist, files , net)
        hs = imhist(rgb2gray(imresize(im,[50 50])));
        cls = vec2ind(net(hs));
        [~, n] = size(hist);
        for i = 1 : n
            if(cls == vec2ind(net(hist(:, i))))
                figure('name', files(i).name);
                imshow(imresize(imread(files(i).name), [100 100]))
  3. Download a picture of a koala and save it outside your MATLAB path as koalatest.jpg
  4. At the MATLAB command prompt type scan(imread(‘[replace with path to koalatest]\koalatest.jpg’)
  5. After a minute or two the networks should have been trained and a figure displaying the matching koala.jpg image shown to you.

NOTE: As explained above this is hacking, not production code I wrote this up in about 20 minutes as a demonstration for classification of images, with imagination this could be extended to classify things like sound for example using a feature map crated from humming a tune to find a song with a similar melody.

LVQ Been trained: