23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exchanging Files over the Internet<br />

Example — Using the urlread Function<br />

The following procedure demonstrates how to retrieve the contents of the Web<br />

page containing the Recent File list at the <strong>MATLAB</strong> Central File Exchange,<br />

http://www.mathworks.com/matlabcentral/fileexchange/index.jsp. It<br />

assigns the results to a string variable, recentFile, and it uses the strfind<br />

function to search the retrieved content for a specific word:<br />

1 Retrieve the Web page content with the urlread function:<br />

recentFile =<br />

urlread('http://www.mathworks.com/matlabcentral/fileexchange/<br />

loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0');<br />

2 After retrieving the content, run the strfind function on the recentFile<br />

variable:<br />

hits = strfind(recentFile,'Simulink');<br />

IfthefilecontainsthewordSimulink, <strong>MATLAB</strong> will store the matches<br />

in the hits variable.<br />

While you can manually pass arguments using the URL, the urlread<br />

function also lets you pass parameters to a Web page using standard HTTP<br />

methods, including post and form. Using the HTTP get method, which<br />

passes parameters in the URL, the following code queries Google for the<br />

word Simulink:<br />

s =<br />

urlread('http://www.google.com/search','get',{'q','Simulink'})<br />

For more information, see the urlread reference page.<br />

Example — Using the urlwrite Function<br />

The following example builds on the procedure in the previous section. This<br />

example still uses urlread and checks for a specific word, but it also uses<br />

urlwrite to save the file if it contains any matches:<br />

% The urlread function loads the contents of the Web page into<br />

the % <strong>MATLAB</strong> workspace.<br />

recentFile =<br />

6-117

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!