This is a static copy of a profile report

Home

urlwrite (4 calls, 17.044 sec)
Generated 05-Nov-2014 07:52:30 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/iofun/urlwrite.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
demo3D11function4
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
132
isc.copyStream(inputStream,fil...
417.004 s99.8%
135
output = char(file.getAbsolute...
40.020 s0.1%
109
location = fullfile(pwd,locati...
40.020 s0.1%
144
status = 1;
40 s0%
134
fileOutputStream.close;
40 s0%
All other lines  0.000 s0.0%
Totals  17.044 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...lwidgets.io.InterruptibleStreamCopierJava method417.004 s99.8%
opaque.charfunction40.010 s0.1%
fullfilefunction40.010 s0.1%
...ttp.HttpURLConnection$HttpInputStreamJava method40 s0%
...t.www.protocol.http.HttpURLConnectionJava method40 s0%
java.io.FileOutputStreamJava method80 s0%
pwdfunction40 s0%
java.io.FileJava method200 s0%
iofun/private/urlreadwritefunction40 s0%
usejavafunction40 s0%
Self time (built-ins, overhead, etc.)  0.020 s0.1%
Totals  17.044 s100% 
Code Analyzer results
Line numberMessage
71The variable 'urlChar' appears to change size on every loop iteration. Consider preallocating for speed.
97Best practice is for CATCH to be followed by an identifier that gets the error information.
116Best practice is for CATCH to be followed by an identifier that gets the error information.
123Best practice is for CATCH to be followed by an identifier that gets the error information.
136Best practice is for CATCH to be followed by an identifier that gets the error information.
Coverage results
[ Show coverage for parent directory ]
Total lines in function144
Non-code lines (comments, blank lines)57
Code lines (lines that can run)87
Code lines that did run34
Code lines that did not run53
Coverage (did run/can run)39.08 %
Function listing
   time   calls  line
1 function [output,status] = urlwrite(urlChar,location,method,params)
2 %URLWRITE Save the contents of a URL to a file.
3 % URLWRITE(URL,FILENAME) saves the contents of a URL to a file. FILENAME
4 % can specify the complete path to a file. If it is just the name, it will
5 % be created in the current directory.
6 %
7 % F = URLWRITE(...) returns the path to the file.
8 %
9 % F = URLWRITE(...,METHOD,PARAMS) passes information to the server as
10 % part of the request. The 'method' can be 'get', or 'post' and PARAMS is a
11 % cell array of param/value pairs.
12 %
13 % [F,STATUS] = URLWRITE(...) catches any errors and returns the error code.
14 %
15 % Examples:
16 % urlwrite('http://www.mathworks.com/',[tempname '.html'])
17 % urlwrite('ftp://ftp.mathworks.com/README','readme.txt')
18 % urlwrite(['file:///' fullfile(prefdir,'history.m')],'myhistory.m')
19 %
20 % From behind a firewall, use the Preferences to set your proxy server.
21 %
22 % See also URLREAD.
23
24 % Matthew J. Simoneau, 13-Nov-2001
25 % Copyright 1984-2008 The MathWorks, Inc.
26 % $Revision: 1.4.4.13 $ $Date: 2010/08/23 23:10:48 $
27
28 % This function requires Java.
4 29 if ~usejava('jvm')
30 error(message('MATLAB:urlwrite:NoJvm'));
31 end
32
33 import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
34
35 % Be sure the proxy settings are set.
4 36 com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings
37
38 % Check number of inputs and outputs.
4 39 error(nargchk(2,4,nargin))
4 40 error(nargoutchk(0,2,nargout))
4 41 if ~ischar(urlChar)
42 error('MATLAB:urlwrite:InvalidInput','The first input, the URL, must be a character array.');
43 end
4 44 if ~ischar(location)
45 error('MATLAB:urlwrite:InvalidInput','The second input, a filename, must be a character array.');
46 end
4 47 if (nargin > 2) && ~strcmpi(method,'get') && ~strcmpi(method,'post')
48 error('MATLAB:urlwrite:InvalidInput','Second argument must be either "get" or "post".');
49 end
50
51 % Do we want to throw errors or catch them?
4 52 if nargout == 2
53 catchErrors = true;
4 54 else
4 55 catchErrors = false;
4 56 end
57
58 % Set default outputs.
4 59 output = '';
4 60 status = 0;
61
62 % GET method. Tack param/value to end of URL.
4 63 if (nargin > 2) && strcmpi(method,'get')
64 if mod(length(params),2) == 1
65 error('MATLAB:urlwrite:InvalidInput','Invalid parameter/value pair arguments.');
66 end
67 for i=1:2:length(params)
68 if (i == 1), separator = '?'; else separator = '&'; end
69 param = char(java.net.URLEncoder.encode(params{i}));
70 value = char(java.net.URLEncoder.encode(params{i+1}));
71 urlChar = [urlChar separator param '=' value];
72 end
73 end
74
75 % Create a urlConnection.
4 76 [urlConnection,errorid,errormsg] = urlreadwrite(mfilename,urlChar);
4 77 if isempty(urlConnection)
78 if catchErrors, return
79 else error(errorid,errormsg);
80 end
81 end
82
83 % POST method. Write param/values to server.
4 84 if (nargin > 2) && strcmpi(method,'post')
85 try
86 urlConnection.setDoOutput(true);
87 urlConnection.setRequestProperty( ...
88 'Content-Type','application/x-www-form-urlencoded');
89 printStream = java.io.PrintStream(urlConnection.getOutputStream);
90 for i=1:2:length(params)
91 if (i > 1), printStream.print('&'); end
92 param = char(java.net.URLEncoder.encode(params{i}));
93 value = char(java.net.URLEncoder.encode(params{i+1}));
94 printStream.print([param '=' value]);
95 end
96 printStream.close;
97 catch
98 if catchErrors, return
99 else error('MATLAB:urlwrite:ConnectionFailed','Could not POST to URL.');
100 end
101 end
102 end
103
104 % Specify the full path to the file so that getAbsolutePath will work when the
105 % current directory is not the startup directory and urlwrite is given a
106 % relative path.
4 107 file = java.io.File(location);
4 108 if ~file.isAbsolute
0.02 4 109 location = fullfile(pwd,location);
4 110 file = java.io.File(location);
4 111 end
112
113 % Make sure the path isn't nonsense.
4 114 try
4 115 file = file.getCanonicalFile;
116 catch
117 error('MATLAB:urlwrite:InvalidOutputLocation','Could not resolve file "%s".',char(file.getAbsolutePath));
118 end
119
120 % Open the output file.
4 121 try
4 122 fileOutputStream = java.io.FileOutputStream(file);
123 catch
124 error('MATLAB:urlwrite:InvalidOutputLocation','Could not open output file "%s".',char(file.getAbsolutePath));
125 end
126
127 % Read the data from the connection.
4 128 try
4 129 inputStream = urlConnection.getInputStream;
130 % This StreamCopier is unsupported and may change at any time.
4 131 isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
17.00 4 132 isc.copyStream(inputStream,fileOutputStream);
4 133 inputStream.close;
4 134 fileOutputStream.close;
0.02 4 135 output = char(file.getAbsolutePath);
136 catch
137 fileOutputStream.close;
138 delete(file);
139 if catchErrors, return
140 else error('MATLAB:urlwrite:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.');
141 end
142 end
143
4 144 status = 1;