MATLAB Answers

Editing Text Strings through Looping

3 views (last 30 days)
James Higgins
James Higgins on 28 Oct 2020
Answered: Geoff Hayes on 10 Nov 2020
Hello Matlab community,
I am new to Matlab programming and have a question surrounding editing text strings through looping.
I am working with API datasearch tool and would like to loop through various text strings. How would I go about doing this?
For example, below is standard Matlab API code for a nonexistant website example. In this example I am telling Matlab to collect from this website a specific data array with code=587. Then I move onto the next code=612.
api = 'http://examplewebsite.com/api'
url = [api 'max=1000/type=fl/sign=xy/code=587/st=87']
S = webread(url)
api = 'http://examplewebsite.com/api'
url = [api 'max=1000/type=fl/sign=xy/code=612/st=87']
S = webread(url)
If I had a list of codes I wanted Matlab to collect data for, such as
d = [587 612 876 296 137 635 591]
Could I create a loop where all I change to the string 'max=1000/type=fl/sign=xy/code=587/st=87' is just the code=587 to code=612...up to 591?
The goal would be to get 7 different S matrices corresponding to the 7 codes in one step.
Thank you,
James

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Nov 2020
James - you could use sprintf to create your url with the "code"
for code = [587 612 876 296 137 635 591]
url = sprintf('http://examplewebsite.com/apimax=1000/type=fl/sign=xy/code=%d/st=87', code)
S = webread(url);
% save S to larger matrix or cell array
end

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!