Reputation: 13
I am trying to create a ulr using sprintf. To open various websites I changed part of the URL using sprintf. Now the following code writes 3times the url instread of replacing part of the url????Any suggestions?Many thanks!!
current_stock = 'AAPL';
current_url = sprintf('http://www.finviz.com/quote.ashx?t=%d&ty=c&ta=0&p=d',current_stock)
web(current_url, '-browser')
%d should be the place holer for appl. Result is :
http://www.finviz.com/quote.ashx?t=65&ty=c&ta=0&p=dhttp://www.finviz.com/quote.ashx?t=65&ty=c&ta=0&p=dhttp://www.finviz.com/quote.ashx?t=80&ty=c&ta=0&p=dhttp://www.finviz.com/quote.ashx?t=76&ty=c&ta=0&p=d
Upvotes: 0
Views: 280
Reputation: 882146
I'm not sure why you're using %d
for a value that is clearly a string? You should be using %s
.
The reason you're seeing what you're seeing is that it appears to be giving you a copy of your format string for each character in the AAPL
string.
You can see that the differences lie solely in the ?t=XX
bit, with XX
being, in sequence, 65, 65, 80 and 76, the ASCII codes for the four letters in your string:
vv
http://www.finviz.com/quote.ashx?t=65&ty=c&ta=0&p=d
http://www.finviz.com/quote.ashx?t=65&ty=c&ta=0&p=d
http://www.finviz.com/quote.ashx?t=80&ty=c&ta=0&p=d
http://www.finviz.com/quote.ashx?t=76&ty=c&ta=0&p=d
^^
Whether that's a feature or bug in MatLab (a), I couldn't say for sure, but I suspect it'll fix itself if you just use the correct format specifier.
(a) It's probably a feature since it does similarly intelligent stuff with other mismatches, as per here:
If you apply a string conversion (
%s
) to integer values, MATLAB converts values that correspond to valid character codes to characters. For example,'%s'
converts[65 66 67]
toABC
.
Upvotes: 3
Reputation: 2750
I would follow this easy way:
current_stock = 'AAPL';
current_url = ['http://www.finviz.com/quote.ashx?t=%d&ty=c&ta=0&p=d',current_stock];
web(current_url,'-browser')
That redirected me to a valid webpage.
Upvotes: 0