Is there a built-in way to build a GET url in Matlab from a set of parameters?

14 views (last 30 days)
0 down vote favorite
I'm trying to get data using an API that takes GET requests. The data are returned in XML format, so I'm hoping to use the xmlread function, which can take a URL as input. However, because the data comes from an API, I need to build the URL with the GET request's arguments and use that as the input for xmlread.
I don't want to use the urlread and urlwrite functions for these reasons:
1. If I use urlread, I get the data as a string, which can't be passed into xmlread.
2. If I use urlwrite, I have to write the data to a file and read it in from there, using xmlread, which is horribly inefficient when my functions make hundreds of API calls.
Is there a built-in Matlab function that allows me to pass in a URL and a cell array (or some other data type) of (argument_name, value) pairs and build a get URL from that? I'm assuming that if a canned method exists in basic Matlab, it will be more efficient than me writing my own that will be rife with string concatenation.

Accepted Answer

Cedric Wannaz
Cedric Wannaz on 1 Feb 2013
Edited: Cedric Wannaz on 1 Feb 2013
If you just want to build a string from multiple components, you can use sprintf, e.g.
>> v = 3 ;
>> subfolder = 'WWW' ;
>> s = sprintf('GET', v, subfolder)
s =
The formatSpec of sprintf will provide you with a lot of flexibility.
>> doc sprintf
Cedric Wannaz
Cedric Wannaz on 1 Feb 2013
Edited: Cedric Wannaz on 1 Feb 2013
As you know, when C is a cell array, C{:} is a comma separated list.. which is somehow the same thing as what you pass to functions when you do a function call (comma separated list of args). So, taking a simpler example, it is "equivalent" to evaluate this
>> C = {2,3,4,5} ;
>> sprintf( '%d_%d,', C{:} )
ans =
and this
>> sprintf( '%d_%d,', 2, 3, 4, 5 )
ans =
Now the way the format generally taken into account, is that the interpreter expects to find as many extra args as there are format conversion sequences (%..), which means in our case 2, and treat (convert) each extra arg according to the corresponding sequence in the format string. Now MATLAB is a little more flexible than that, in the sense that it will loop over the format as many times as needed to display all the args (it's cyclic), so your guess was right. In our case, you can see that because I used different chars in the middle of the format and at the end. If the number of extra args is not a multiple of the number or conversion sequences/codes, the last cycle is interrupted before the end, e.g.
>> sprintf( '%d_%d,', 2, 3, 4, 5, 6 )
ans =
Here, you have 2 cycles and a half. This is what happens in your case, and you can see that you don't have only the last ? that is wrong, but also the one in the middle.
You have many options for correcting that, but the simplest is probably to have sprintf work only on the args, and to concatenate this with the first part of the URL.. e.g.
>> str = ['', sprintf('%s=%s&', C{:})]
str =

Sign in to comment.

More Answers (1)

mansour torabi
mansour torabi on 1 Oct 2021
Yes, there is!
To create a URI query string form structure variable (or some other types):
% Define your query Data as a structure variable
sVar.Param1 = 'Value1';
sVar.Param2 = 'Value2';
EncodedURL = char(
EncodedURL = 'Param1=Value1&Param2=Value2'



Community Treasure Hunt

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

Start Hunting!