You are on page 1of 2

Matlab exercises, 5th lesson

External interfaces
1. Use Matlabs Java interface to write a script that can be used to read todays menu of Teekkariravintolat (a student restaurant) from the web address http://ruokalistat.net/. Hints: Create an object for reading the source code of the web page as in the lecture slides. Read the html code in your script line by line until you find the word Teekkariravintolat (findstr function helps). Read the lines with readLine. The Java string returned by readLine can be converted to a Matlab string with function char. After this find the next instance of the word ruokateksti which is used to define styles on the web page. Read and save the following lines to one long string until you encounter the next </td> tag in the html code (end tag of a cell in the table). Save also the line that contains the </td> tag. Familiarize yourself with function strrep and use it to replace the </td> tags with an empty string () and the <BR> tag with a carriage return character (char(10)) in the string you saved. Add some titles to the string (see example below) and print it to the screen with disp.

Example run:
This menu has been retrieved from http://ruokalistat.net/ Teekkariravintolat offers today (30.03.2005): Lihamakaronilaatikkoa (vl) Hirssi-kaaliraitaa jogurttikastikkeella (vl,g) Aurajuustokeittoa (vl) Kreikkalaista salaattia A`la Carte: Grillilohta tartarkastikkeella (vl)

2. Do the COM object example in the lecture slides. After that edit the code so that you feed from Matlab a 50x10 matrix (range A1 J50 in Excel) of random numbers to the first sheet of Excels workbook. The random numbers are created in Excel, not in Matlab! This can be done by feeding the string =rand() from Matlab to each cell in Excel. Finally read the random numbers from the range C10 H30 in Excel to Matlab variable B and print the numbers to the screen. On the left there is a picture of the first sheet of the Excel workbook after the string = rand() has been fed to the cells. An example of the contents of variable B can be seen on the next page. Note that the numbers are random, so it is unlikely that you get exactly same numbers.

B= 0.0949 0.8786 0.3700 0.5662 0.8677 0.7486 0.2406 0.3228 0.8956 0.4854 0.5099 0.8757 0.1390 0.8173 0.7946 0.0328 0.4867 ... 0.6443 0.7373 0.0178 0.8341 0.8033 0.5627 0.8743 0.7738 0.4651 0.9230 0.3284 0.7360 0.8057 0.2526 0.9919 0.1382 0.1581 0.3280 0.4442 0.6428 0.3359 0.7759 0.0929 0.7679 0.3389 0.9150 0.1857 0.3608 0.7854 0.6227 0.2495 0.7446 0.3882 0.6766 0.1228 0.5272 0.5778 0.0323 0.2783 0.4307 0.9418 0.7374 0.6912 0.7045 0.8828 0.7025 0.6324 0.3272 0.6511 0.5033 0.7196 0.6936 0.1985 0.4585 0.2884 0.8549 0.5467 0.6578 0.2349 0.1176 0.7220 0.5075 0.6977 0.8432 0.4460 0.6545 0.5255 0.7620 0.4334 0.3674 0.4891 0.3874 0.1304 0.0758 0.3984 0.0450 0.6002 0.4452 0.9061 0.6879 0.6946 0.8503 0.3946 0.9279 0.2649

3. Download the file measurements.zip from the timetable page of the course and extract it to your working directory. The zip file contains measurement data files that have the following structure: (the beginning of file data00009.txt)
Wed 4 Wed 5 Wed 3 30.3.2004 9:00 5.881671 50.508846 30.3.2004 9:01 6.667738 47.986359 30.3.2004 9:02 4.996766 48.546884 14.306377 16.097644 13.771273

(etc.) Every other line contains a timestamp and every other the actual measurements. The names of the files are numbered with consecutive numbers but some files are missing. Write a script that reads all the measurement files and saves to a file called allData.txt a summary of the measurements. The summary file should contain on each row the name of the original measurement file and then the two last measurements of each row with the precision of two decimals. The measurements above would look like this.
data00009.txt data00009.txt data00009.txt 50.51 47.99 48.55 14.31 16.10 13.77

(etc.) Hints: A file can be opened for reading and writing with function fopen. Find the necessary parameters from Matlabs help. In the end the files must be closed with fclose. Use a for loop having as indexes the numbers of the measurement files (for file = [9 11 12 13 16]). In this way you can process a large amount of systematically named files. Create the name of the file to be read with sprintf, open the file and read it line by line with fgetl. Write the required data to the summary file.

You might also like