It has been a while since my last MATLAB post so…
I was trying to retrieve data from the National Weather Service for another post when I found a protocol.
The protocol is called SOAP (Simple Object Access Protocol). In simple words, It defines an standard procedure to communicate information between different computers over internet. (I’m not an expert, so I’m basically learning while writing this).
So, let’s make it simple. Suppose we have two computers, HOME and DATA_SERVER. We have a program in HOME that wants to retrieve some data from DATA_SERVER, maybe raw data and some basic operation as counting the number of elements. How to do it? The HOME computer can send an XML document to DATA_SERVER and DATA_SERVER will read that document and execute the instructions in it. Most probably, returning some data. An XML document is simple a kind of text file that uses indentation and mixes code with normal language. Something like this:
Now, because it will be a complete mess with many different systems and programs and ways of writting code, there is an standard on how to writte that XML document, is called SOAP. SOAP describes a standard way of writting the XML document so it can be understanded by a wide range of programs, and if there is some update in the program, the XML will still work. A XML document written according to SOAP will look something like this:
Or being more specific, something like…
<pre class="de1">POST /InStock HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
<span class="sc3"><span class="re1"><?xml</span> <span class="re0">version</span>=<span class="st0">"1.0"</span><span class="re2">?></span></span>
<span class="sc3"><span class="re1"><soap:Envelope</span> <span class="re0">xmlns:soap</span>=<span class="st0">"http://www.w3.org/2003/05/soap-envelope"</span><span class="re2">></span></span>
<span class="sc3"><span class="re1"><soap:Header<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"></soap:Header<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"><soap:Body<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"><m:GetStockPrice</span> <span class="re0">xmlns:m</span>=<span class="st0">"http://www.example.org/stock"</span><span class="re2">></span></span>
<span class="sc3"><span class="re1"><m:StockName<span class="re2">></span></span></span>IBM<span class="sc3"><span class="re1"></m:StockName<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"></m:GetStockPrice<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"></soap:Body<span class="re2">></span></span></span>
<span class="sc3"><span class="re1"></soap:Envelope<span class="re2">></span></span></span></pre>
I’m trying to keep it as low and understandable as possible.
Now, how to create this SOAP documents?
Well, in fact there is an easy way for HOME to create the SOAP document for sending it to DATA_SERVER.
A part from SOAP, we have WSDL (Web Services Description Language). WSDL is a XML document that describes all the instruction that a machine/program can accept and how to call them. Basically WSDL is a document to tell HOME what kind of instructions to send to DATA_STORAGE inside the SOAP document. At the same time, it is standard and it’s usage can be automated, so you can have a program in HOME computer that analizes data from internet sources. If a new source appears it will have it’s own WSDL document telling you how to access it’s data using a SOAP document. You simply download that WSDL document and asks your computer to generate the code.
Ok ok, sounds nice… AN EXAMPLE PLEASE!!!!
MATLAB Real-Time currency converter
The description I just made is a little bit simple, but should be enough to understand how this things work. In the end, there is a set of nodes, paths, intermediates… that read the SOAP documents, interpret them and redirects everything.
Let’s just say that one source of WSDL documents to access several different data bases is WebserviceX.NET
In this web page you can have WSDL documents for many data bases. The one we are interested now is the currency exchange one
The currency exchange WSDL will tell our program (in this case MATLAB) how to ask for the actual currency exchange. When we click on the link… We acces a webpage with the WSDL link.
Step 1: Just copy the link to the WSDL document to use it in MATLAB: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
Now go to MATLAB. The documentation that will help us is Access Web Services Using MATLAB SOAP Functions.
Step 2: Use the WSDL file to create a MATLAB class.
It will create a folder for the class on your working directory (it will be named @CurrencyConvertor).
Step 3: Create an object of the new class (the creation name of the class will be the name of the directory, which will have a m-file inside with the same name, the rest of m-files will be procedures to be used with the object ).
I cannot explain here what a class in MATLAB means, but suppose you want to create vectors and tell MATLAB how to work with a vector. You will define the class vector which has fields, properties etc… in the simple case, just a set of numbers. Then appart from that definitions, you define procedures to work with vectors. One procedure will be for instance how to add 2 vectors (for instance adding together the set of numbers).
Step 4: If you go into the created class directory, you will find 3 m-files, if you open them, one if to create the class, one to display the information of the WSDL and the other one called ConversionRate is to request the conversion rates. It’s just a function to be used with the object we just created “x”. And the description tells us how to use it. For instance, retrieving the conversion rate between USA Dollar and United Kingdom Pound:
ConversionRateResult = ConversionRate(x,'USD','GBP')
And the answer is 0.5948 is it correct? According to Google, it is:
MATLAB Stock Exchange Quote
This one will tell us how to get real data from the stock exchange.
I think many people will find this one extremely interesting.
Step 1: Get the WSDL file from WebserviceX
Step 2: As before create a class:
Step 3: Create an object of that class
and use it to get the stock value of… “IBM” (of course, you need to look for the names of the stock values, and one good place to do that is Yahoo Finances)
Step 4: Get the stock value of IBM. As before, checking the class folder we see that GetQuote.m is the file to get the data.
This time the answer is a string with text and values: <StockQuotes><Stock><Symbol>IBM</Symbol><Last>186.37</Last><Date>6/6/2014</Date><Time>4:01pm</Time><Change>+0.39</Change><Open>186.47</Open><High>187.65</High><Low>185.90</Low><Volume>3296900</Volume><MktCap>188.6B</MktCap><PreviousClose>185.98</PreviousClose><PercentageChange>+0.21%</PercentageChange><AnnRange>172.19 – 206.98</AnnRange><Earns>14.626</Earns><P-E>12.72</P-E><Name>International Bus</Name></Stock></StockQuotes>
This string can be scanned very easily to get only the desired data.
For the last example I’m going to use a different web. It’s called Xmethods and it’s a repository for WSDL links.
The last example is…
MATLAB to compare images in internet
Basically this is going to take 2 urls to 2 images and compare them and give a number. The closer to zero the closer are the images.
Step 1: Get the WSDL which is: http://www.quisque.com/fr/techno/eqimage/eqimage.asmx?WSDL
Step 2: Create the class and one object of the class.
Step 3: Compare this 3 images:
A (url: http://thegoodride.org/wp-content/uploads/2014/04/goonies_map_2010_a_l.jpg)
B (url: http://www.blogcdn.com/blog.moviefone.com/media/2010/06/gooniesmainscenes.jpg)
C (url: http://media0.giphy.com/media/ftvw5gPRJzdcc/200_s.gif)
&nAnd the code is
which gives us the results…
To be honest…. I think it is more usefull in terms of knowing if the image is exactly the same, but not so good for comparison.
Hope you like this short introduction.
What? Want more?
What I showed you was very nice… but It could be even better. Think about it commercially. Ignite is the answer.
Ignite is the same as WebserviceX.NET but commercial, that means that you will have access to many more data bases and information. Energy demand, currency income into companies, news headlines… but of course you have to pay to get that information. You need to use a login in your SOAP files.