Using RESTful Web Service Interface in R2014b MATLAB
FT
Contents
Introduction
In R2104b, MATLAB introduced three new features to access RESTful web services:
- webread - Read content from RESTful web service
- websave - Save content from RESTful web service to file
- weboptions - Specify parameters for RESTful web service
To illustrate the use of these features, let's do a thought experiment and imagine that you are interested in applying for a software development job at MathWorks. This blog post illustrates how you can use RESTful web services to help in your job search. In particular it shows you how to:
- Obtain job listings from an RSS feed,
- Use the Facebook Graph API to obtain information about MathWorks,
- Obtain current weather conditions and airport delays,
- Map your route from Boston Logan to MathWorks, and
- Obtain and display a high-resolution image of the MathWorks campus
Obtain Job Listings
The first step in your job search is to obtain a listing of all available jobs for your desired location. Job listings are available in an RSS feed, and you can use the function webread to read the feed. The RSS feed is an XML format. Specify 'xmldom' content type in order to return a Java Document Object Model for easy access to the data. Read the RSS feed and parse the data to create a listing of jobs of interest.
options = weboptions('ContentType','xmldom'); rssURL = 'https://www.mathworks.com/company/jobs/opportunities/rss.xml'; dom = webread(rssURL, options);
Retrieve all the items from the DOM that are tagged 'item'.
items = dom.getElementsByTagName('item');
itemsLength = items.getLength;
Create a table to contain the information from the RSS feed. Map the XML tag names to the table variable names by using two cell arrays. Initialize the table using cell2table.
tagNames = {'id', 'location', 'title', 'job_function', 'job_type'}; tableVariableNames = {'Reference', 'Location', 'Title', 'Department', 'Type'}; emptyItems = cell(itemsLength,length(tableVariableNames)); jobsTable = cell2table(emptyItems,'VariableNames',tableVariableNames);
Loop through each item and add the information to the table. Use zero-based indexing to obtain the node since the data is contained in a Java object. Use one-based indexing for the table array.
for k=0:itemsLength-1 item = items.item(k); for n = 1:length(tagNames) node = item.getElementsByTagName(tagNames{n}); jobsTable{k+1,n} = {char(node.item(0).getTextContent)}; end end
Select your desired location.
myLocation = 'US-MA-Natick';
rows = strcmpi(jobsTable.Location, myLocation);
natickJobs = jobsTable(rows,:);
natickJobs.Location = [];
Select your desired department.
myDepartment = 'software development';
rows = strcmpi(natickJobs.Department, myDepartment);
natickSoftwareJobs = natickJobs(rows,:);
natickSoftwareJobs.Department = [];
Display a table of possible jobs that match your request.
natickSoftwareJobs
natickSoftwareJobs = Reference Title Type _________ _________________________________________________________________________ ___________________ '14070' 'MATLAB Editor Software Engineer (Senior) (14070-MCAR)' 'Experienced' '14064' 'Software Engineer - Graphical Platform (14064-BWAL)' 'Experienced' '14049' 'Release Engineer (14049-BHIL)' 'Experienced' '14028' 'Software Engineer (14028-MCOL)' 'Experienced' '14027' 'Software Engineer (14027-MCOL)' 'Experienced' '14023' 'Software Developer (14023-MCOL)' 'Experienced' '13866' 'Controls Systems Engineer (13866-MCAR)' 'Experienced' '13859' 'Software Engineer (13859-MCOL)' 'Experienced' '13857' 'Web User Interface Developer (13857-MCOL)' 'Experienced' '13856' 'Senior Software Engineer (13856-MCOL)' 'Experienced' '13855' 'Software Engineer (13855-MCOL)' 'Experienced' '13854' 'Software Engineer - GUI Components (13854-MCOL)' 'Experienced' '13853' 'Build and Release Engineer - Perl / Shell (13853-MCOL)' 'Experienced' '13851' 'Code Replacement Library Software Engineer (13851-GMAR)' 'Experienced' '13848' 'Senior Performance Engineer (13848-MCAR)' 'Experienced' '13793' 'Software Engineer - MATLAB Development Tools (13793-MCAR)' 'Experienced' '13792' 'Software Engineer - Desktop / Toolbox Integration (13792-MCAR)' 'Experienced' '13791' 'Autonomous Controls Systems Engineer (13791-MCAR)' 'Experienced' '13789' 'Software Engineer - MATLAB Hardware (13789-MCAR)' 'Experienced' '13779' 'Web Application Developer (13779-JJUS)' 'Experienced' '13778' 'Software Engineer - Cloud Storage (13778-JJUS)' 'Experienced' '13777' 'Senior Software Engineer - Web Services (13777-JJUS)' 'Experienced' '13776' 'Senior Software Engineer - MATLAB Online (13776-JJUS)' 'Experienced' '13775' 'Senior Technical Lead - MATLAB Web Services (13775-JJUS)' 'Experienced' '13774' 'Java Software Engineer - Support Software Installer (13774-JJUS)' 'Experienced' '13773' 'Java Software Engineer (13773-JJUS)' 'Experienced' '13772' 'Mobile Software Engineer (13772-JJUS)' 'Experienced' '13752' 'Software Engineer - Numerical Optimization (13752-SMAR)' 'Experienced' '13751' 'Principal Software Engineer - Data Munging (13751-SMAR)' 'Experienced' '13750' 'Senior Software Engineer - MATLAB Math (13750-SMAR)' 'Experienced' '13747' 'Image Algorithms Software Engineer (13747-SMAR)' 'Experienced' '13746' 'Software Engineer - Data Analysis (13746-SMAR)' 'Experienced' '13745' 'Data Analysis Tools Developer (13745-SMAR)' 'Experienced' '13743' 'Senior Software Engineer - Statistics and Machine Learning (13743-SMAR)' 'Experienced' '13742' 'Senior MATLAB Web UI Engineer (13742-SMAR)' 'Experienced' '13734' 'Senior Software Engineer - Application Deployment (13734-MCAR)' 'Experienced' '13733' 'Software Engineer - Compiler Componentization (13733-MCAR)' 'Experienced' '13732' 'UI Developer - Toolboxes & Addons (13732-MCAR)' 'Experienced' '13730' 'Software Engineer - C+ (13730-MCAR)' 'Experienced' '13729' 'Software Engineer - C++ (13729-MCAR)' 'Experienced' '13689' 'Simulink Developer Engineer (13689-KCAR)' 'Internships' '13668' 'Co-Op: Design Automation Software Development (13668-KCAR)' 'Internships' '13660' 'C++ Software Engineer Algorithms and Code Generation (13660-GMAR)' 'Experienced' '13651' 'Compiler Engineer for FPGA Design (13651-KCAR)' 'Internships' '13621' 'C++ Software Engineer - User Interfaces and Visualization (13621-BWAL)' 'Experienced' '13613' 'MATLAB Web Applications Engineer - Intern (13613-JJUS)' 'Internships' '13603' 'C++ Software Engineer Intern (13603-KCAR)' 'Internships' '13594' 'Senior MATLAB Test Frameworks Engineer (13594-BHIL)' 'Experienced' '13593' 'Software Engineer for Design Automation (13593-KKOT)' 'Internships' '13577' 'Data Tools Development Intern (13577-KCAR)' 'Internships' '13569' 'Senior Compiler Engineer (13569-GMAR)' 'Experienced' '13566' 'Simulink Compiler Engineering Intern (13566-KCAR)' 'Internships' '13563' 'LRM Updates to Verilog and SystemVerilog Emitters (13563-KCAR)' 'Internships' '13558' 'Software Engineer GUI and Hardware Connectivity (13558-GMAR)' 'Experienced' '13545' 'C++ Software Engineer - Graphical User Interfaces (13545-BWAL)' 'Experienced' '13538' 'OAuth / LTI Integration Software Intern (13538-KCAR)' 'Internships' '13532' 'Software Scope Infrastructure Internship (13532-KCAR)' 'Temps/Consultants' '13531' 'Software Scope Infrastructure Internship (13531-KCAR)' 'Temps/Consultants' '13530' 'FPGA Prototyping Intern (13530-KCAR)' 'Internships' '13529' 'HDL Verifier Intern (13529-KCAR)' 'Internships' '13518' 'Communications and Signal Processing HDL Development (13518-KCAR)' 'Internships' '13516' 'FPGA Prototyping for Image, Video and Computer Vision (13516-KCAR)' 'Internships' '13515' 'Audio Intern (13515-KCAR)' 'Internships' '13508' 'C++ Software Developer - Discrete Event Simulation (13508-GMAR)' 'Experienced' '13473' 'C++ Developer (13473-MCAR)' 'Experienced' '13466' 'JavaScript Software Engineer (13466-SMAR)' 'Experienced' '13422' 'Release Engineering Intern (13422-KCAR)' 'Internships' '13412' 'Web Applications Development Intern (13412-KCAR)' 'Internships' '13403' 'Documentation Toolsmith (13403-JJUS)' 'Experienced' '13393' 'Robotics Software Developer (13393-GMAR)' 'Experienced' '12383' 'C++ Software Engineer - Compiler Engineer for MATLAB (12383-MCAR)' 'Experienced' '12382' 'Senior Compiler Engineer LLVM (12382-GMAR)' 'Experienced' '12379' 'Senior Software Engineer - Internet Services Platform (12379-JJUS)' 'Experienced' '12377' 'Senior Software Engineer - Graphics / OpenGL (12377-SMAR)' 'Experienced' '12369' 'Build System Software Engineer (12369-BHIL)' 'Experienced' '12365' 'Test Infrastructure Software Developer (12365-BHIL)' 'Experienced' '12362' 'Software Engineer - Data Analysis Tools (12362-SMAR)' 'Experienced' '12358' 'Web UI Developer - MATLAB Editor (12358-MCAR)' 'Experienced' '12356' 'Software Developer - Graphic Export and Printing (12356-SMAR)' 'Experienced' '12352' 'JavaScript Build Infrastructure Engineer (12352-BHIL)' 'Experienced' '12351' 'Software Engineer - Maven Build and Deployment (12351-BHIL)' 'Experienced' '12342' 'Web Application Developer (12342-JJUS)' 'Experienced' '12324' 'Compiler Engineer - Parallel Computing (12324-GMAR)' 'Experienced' '12322' 'Web UI Software Engineer (12322-SMAR)' 'Experienced' '12320' 'Senior Web Application Developer (12320-JJUS)' 'Experienced' '12317' 'Senior Software Engineer - Enterprise Solutions (12317-MCAR)' 'Experienced' '12316' 'C++ Software Engineer - Simulink Architecture Group (12316-GMAR)' 'Experienced' '12315' 'Computer Vision Systems Software Engineer (12315-GMAR)' 'Experienced' '12312' 'Web Application Developer - Groovy on Grails/Java (12312-BHIL)' 'Experienced' '12308' 'Analog and Mixed Signal Simulation Developer (12308-GMAR)' 'Experienced' '12305' 'Compiler Engineer (12305-GMAR)' 'Experienced' '12304' 'Software Engineer - Data Streaming and Visualization (12304-BWAL)' 'Experienced' '12301' 'C++ Algorithm Optimization Software Engineer (12301-GMAR)' 'Experienced' '12300' 'Senior Compiler Developer - Auto-generate C from Simulink (12300-GMAR)' 'Experienced' '12284' 'C++ Algorithm Developer - Stateflow (12284-GMAR)' 'Experienced' '12275' 'C++ Code Generation Software Engineer (12275-GMAR)' 'Experienced' '12240' 'Java Software Engineer Distributed Application Development (12240-BHIL)' 'Experienced' '12229' 'Senior Engineer, Requirements Modeling and Management (12229-GMAR)' 'Experienced' '12227' 'Senior Engineer, Model Static Analysis (12227-GMAR)' 'Experienced' '12217' 'Release Engineering Intern (12217-BHIL)' 'Internships' '12174' 'Internal Tools Developer (12174-BHIL)' 'Experienced' '12159' 'Release Engineer (12159-BHIL)' 'Experienced' '12155' 'Web Software Test Infrastructure and Architecture Engineer (12155-BHIL)' 'Experienced' '12072' 'Web GUI Engineer (12072-SMAR)' 'Experienced' '12042' 'Web Application Developer - Commerce Systems (12042-JJUS)' 'Experienced' '12029' 'Senior Software Engineer - Graphics / OpenGL (12029-SMAR)' 'Experienced' '11941' 'Numerical Simulation Software Engineer (11941-GMAR)' 'Experienced' '11883' 'Senior Software Engineer, Program Analysis/Formal Methods (11883-GMAR)' 'Experienced' '11715' 'Product Release Engineer (11715-BHIL)' 'New Graduate' '11669' 'Web UI Software Engineer (11669-JJUS)' 'Experienced' '11506' 'Support Package for Mobile devices (11506-SMAR)' 'Internships' '11471' 'DevOps Engineer - Cloud/SaaS (11471-SMAR)' 'Experienced' '11431' 'Senior Compiler Engineer (11431-MCAR)' 'Experienced' '11373' 'Web User Interface - Senior Software Developer (11373-GMAR)' 'Experienced' '11302' 'Senior Technical Lead - Statistics / Machine Learning (11302-SMAR)' 'Experienced' '11254' 'Senior Software Developer - Graphical Language Editors (11254-BWAL)' 'Experienced' '11087' 'Web MATLAB Desktop UI Developer (11087-MCAR)' 'Experienced' '10415' 'Senior C++ Compiler Engineer (10415-MCAR)' 'Experienced' '10089' 'Senior Software Test Infrastructure Developer (10089-BHIL)' 'Experienced' '10038' 'MATLAB Software Engineer - Web GUIs (10038-SMAR)' 'Experienced' '9537' 'Senior Compiler Engineer (9537-MCAR)' 'Experienced' '8916' 'MATLAB on the Web - Backend Developer (8916-JJUS)' 'Experienced' '8904' 'Senior Software Engineer - Web UI Platform (8904-JJUS)' 'Experienced' '8892' 'Simulink Compiler Engineer (8892-GMAR)' 'Experienced'
Perhaps after searching through this list of jobs, you realize that you would prefer an application engineering job. You can do more searches by placing the commands that are illustrated above into a jobsReader function. The jobsReader function takes a filename as the first input followed optionally by the location and department strings. You can specify 'all' for either location or department to get a complete listing. Save the content to a file and then use your jobsReader function. You can save the RSS feed by using websave to save the contents to a file.
filename = 'rss.xml';
websave(filename, rssURL);
Specify your new job search criteria and obtain the data from the file.
myLocation = 'US-MA-Natick'; myDepartment = 'Application Engineering'; applicationEngineeringJobs = jobsReader(filename,myLocation,myDepartment)
applicationEngineeringJobs = Reference Title Type _________ ____________________________________________________________________ _____________ '14075' 'Application Engineer- Hardware-in-the-Loop (14075-SMAR)' 'Experienced' '11969' 'Senior Application Engineer (11969-SMAR)' 'Experienced' '11943' 'Principal Application Engineer - Hardware-in-the-Loop (11943-SMAR)' 'Experienced'
Rather than saving the content to a file, you can specify a function handle as a content reader for webread. When the ContentReader property is specified in weboptions, webread downloads the content to a temporary file and reads the file using the specified function.
Specify an anonymous function handle to return application engineering jobs in Natick.
contentReader = @(filename)jobsReader(filename,myLocation,myDepartment);
options = weboptions('ContentReader',contentReader);
applicationEngineeringJobs = webread(rssURL,options)
applicationEngineeringJobs = Reference Title Type _________ ____________________________________________________________________ _____________ '14075' 'Application Engineer- Hardware-in-the-Loop (14075-SMAR)' 'Experienced' '11969' 'Senior Application Engineer (11969-SMAR)' 'Experienced' '11943' 'Principal Application Engineer - Hardware-in-the-Loop (11943-SMAR)' 'Experienced'
Learn more about MathWorks from Facebook
Use the Facebook Graph API to obtain more information about MathWorks. The Graph API returns a JSON string. webread parses the string into a structure.
facebookAPI = 'https://graph.facebook.com/'; company = 'MathWorks'; facebookURL = [facebookAPI company]; mathworks = webread(facebookURL)
mathworks = id: '19564567448' about: 'Over one million people around the world speak M...' can_post: 0 category: 'Computers/technology' category_list: [1x1 struct] checkins: 2063 company_overview: 'MathWorks products are used throughout the autom...' cover: [1x1 struct] description: 'MATLAB® is a high-level language and interactive...' founded: '1984' has_added_app: 0 is_community_page: 0 is_published: 1 likes: 211377 link: 'https://www.facebook.com/MathWorks' location: [1x1 struct] mission: 'Technology Our goal is to change the world by a...' name: 'MathWorks' parking: [1x1 struct] phone: '(508) 647-7000' talking_about_count: 2268 username: 'MathWorks' website: 'https://www.mathworks.com' were_here_count: 2063
Display overview information about MathWorks. Use line wrapping to display the text; otherwise it will get trimmed when publishing the blog post. Use the function strsplit to split the string at each white space into a cell array of strings. Compute indices indicating which elements to print. With the indices, use the function strjoin to construct the string to print. Use the function strtrim to ensure that each line begins with a character.
splitData = strsplit(mathworks.company_overview,' '); maxLengthOfLine = 80; lengthOfString = cellfun(@length, splitData) + 1; while ~isempty(splitData) elementsToPrint = cumsum(lengthOfString) <= maxLengthOfLine; if ~any(elementsToPrint) elementsToPrint(1) = true; end lineout = strjoin(splitData(elementsToPrint)); fprintf('%s\n', strtrim(char(lineout))); splitData = splitData(~elementsToPrint); lengthOfString = lengthOfString(~elementsToPrint); end
MathWorks products are used throughout the automotive, aerospace, communications, electronics, and industrial automation industries as fundamental tools for research and development. They are also used for modeling and simulation in increasingly technical fields, such as financial services and computational biology. MathWorks software enables the design and development of a wide range of advanced products, including automotive systems, aerospace flight control and avionics, telecommunications and other electronics equipment, industrial machinery, and medical devices. More than 5000 colleges and universities around the world use MathWorks solutions for teaching and research in a broad range of technical disciplines.
Display the MathWorks cover source image from Facebook. webread returns an M-by-N-by-3 image when reading image/jpeg content. Use imshow to display the image.
RGB = webread(mathworks.cover.source); figure imshow(RGB)
You can use webread to read specific fields from Facebook by specifying a 'fields' query parameter. Use the query parameter syntax to read the 'mission' and 'likes' fields.
fields = 'mission,likes'; data = webread(facebookURL,'fields',fields)
data = mission: 'Technology Our goal is to change the world by accelerating ...' likes: 211377 id: '19564567448'
To understand more about MathWorks, print the mission statement. The function strprint is a local function created from the code in the previous section to split and print the company overview. In this case, use strsplit to split the mission statement into sentences.
sentences = strsplit(data.mission, '.'); for k = 1:length(sentences) fprintf('\n') if ~isempty(sentences{k}) sentence = sprintf('%s.\n', sentences{k}); strprint(sentence) end end
Technology Our goal is to change the world by accelerating the pace of discovery, innovation, development, and learning in engineering and science. We work to provide the ultimate computing environment for technical computation, visualization, design, simulation, and implementation. We use this environment to provide innovative solutions in a wide range of application areas. Business We strive to be the leading worldwide developer and supplier of technical computing software. Our business activities are characterized by quality, innovation, and timeliness; competitive awareness; ethical business practices; and outstanding service to our customers. Human We cultivate an enjoyable, vibrant, participatory, and rational work environment that nurtures individual growth, empowerment, and responsibility; appreciates diversity; encourages initiative and creativity; values teamwork; shares success; and rewards excellence. Social We actively support our communities and promote social and environmental responsibility. Learn more about our Social Mission.
Display the location information to assist in travel planning.
location = mathworks.location
location = city: 'Natick' country: 'United States' latitude: 42.3 longitude: -71.349 state: 'MA' street: '3 Apple Hill Dr' zip: '01760'
Use RESTful Web Services to obtain Airport and Weather Information
You can use a RESTful web service from the Federal Aviation Administration (FAA) to find out the local weather and determine whether any flight delays exist for the Boston Logan International Airport. The web service allows you to specify JSON format for the output.
faaURL = 'http://services.faa.gov/airport/status/'; airportCode = 'BOS'; url = [faaURL airportCode]; format = 'application/json'; options = weboptions('Timeout',10); data = webread(url,'format',format,options)
data = delay: 'false' IATA: 'BOS' state: 'Massachusetts' name: 'General Edward Lawrence Logan International' weather: [1x1 struct] ICAO: 'KBOS' city: 'Boston' status: [1x1 struct]
Display the time.
data.weather.meta
ans = credit: 'NOAA's National Weather Service' updated: '8:54 AM Local' url: 'http://weather.gov/'
Display the weather information, including temperature and wind speed.
data.weather
ans = visibility: 2 weather: 'Light Rain Fog/Mist' meta: [1x1 struct] temp: '43.0 F (6.1 C)' wind: 'Northwest at 10.4mph'
Display the airport status.
data.status
ans = reason: 'No known delays for this airport.' closureBegin: '' endTime: '' minDelay: '' avgDelay: '' maxDelay: '' closureEnd: '' trend: '' type: ''
Display Route from Boston Logan to MathWorks
If you have access to the Mapping Toolbox™, you can display a map showing you a route from Boston Logan International Airport to MathWorks. Use the gpxread function to read a sample GPX file that ships with the Mapping Toolbox and contains a route from Boston Logan to MathWorks. Overlay that route onto an OpenSteetMap base layer using the webmap function. Display the route using the wmline function.
route = gpxread('sample_route'); webmap('openstreetmap') wmline(route.Latitude,route.Longitude)
Print out turn-by-turn directions.
turns = route(~cellfun(@isempty, route.Description)); for k = 1:length(turns) fprintf('%s. %s\n',num2str(k),turns(k).Description) end
1. Head southeast 2. Keep left at the fork, follow signs for I-90 W/I-93 S/Williams Tunnel/Mass Pike and merge onto I-90 W Partial toll road 3. Take exit 13 to merge onto MA-30 E/Cochituate Rd toward Natick Partial toll road 4. Turn right onto Speen St 5. Merge onto MA-9 E/Worcester St via the ramp on the left to Boston 6. Slight right onto Apple Hill Dr 7. Turn left toward Apple Hill Dr 8. Take the 1st right onto Apple Hill Dr Destination will be on the right 9. The MathWorks, Inc., Natick, MA.
Display High Resolution Image of MathWorks Campus
Your final destination is the MathWorks campus. You can obtain a high-resolution image of the area from the United States Geological Survey (USGS) National Map Server using the Web Map Service (WMS) protocol. The function webread returns an RGB image when reading 'image/jpeg' content type. Specify the WMS query parameters to obtain the image of interest.
usgsURL = 'http://raster.nationalmap.gov/arcgis/services/Orthoimagery/USGS_EROS_Ortho/ImageServer/WMSServer'; service = 'WMS'; layers = '0'; crs = 'CRS:84'; contentType = 'image/jpeg'; height = 512; width = 512; request = 'GetMap'; latlim = [42.298530964,42.301131366]; lonlim = [-71.353251832,-71.348120766]; format = '%12.9f'; bbox = [ ... sprintf(format,lonlim(1)) ',' sprintf(format,latlim(1)), ',', ... sprintf(format,lonlim(2)) ',' sprintf(format,latlim(2))]; version = '1.3.0'; RGB = webread(usgsURL, ... 'SERVICE', service, 'LAYERS', layers, 'CRS', crs, 'FORMAT', contentType, ... 'HEIGHT', height', 'WIDTH', width, 'REQUEST', request, 'BBOX', bbox);
Display the image.
figure imshow(RGB)
Conclusion
I am hopeful that this post gives you an introduction to accessing RESTful web services using MATLAB. There are many RESTful web services available on the Internet. How might you use these tools in your work? Let us know here.
Best of luck in your job search!
Published with MATLAB® R2014b
- Category:
- New Feature