urlread function: put variable in string

2 次查看(过去 30 天)
I am using the urlread() function to read in information from an url. How could I include variables into the url address?
I have latitude and longitude information which I would like to implement into the url when looping through the data. Here some example for lat and long variables:
lat=43.2;
long=116.3
I tried using num2str(variablename) but this failed with an unexpected matlab error
alt= urlread('http://api.geonames.org/astergdem?lat=' num2string(lat) '&lng=' num2str(long) '&username=test12345&style=full&type=JSON')

采纳的回答

Cedric
Cedric 2015-9-3
编辑:Cedric 2015-9-3
You can concatenate strings using square brackets, but I usually prefer building a final string using SPRINTF, e.g.
url = sprintf( 'http://api.geonames.org/astergdem?lat=%f&lng=%f&username=test12345&style=full&type=JSON', lat, long ) ;
data = urlread( url ) ;
The first string in the call to SPRINTF is called formatSpec. Look in MATLAB doc and you will see what the two %f stand for and how to tailor them to your needs. In your command window, type
doc sprintf
and look for the formatSpec.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by