n=100;
for k=1:1:n
B(k)=Locs(1,k)
C(k)=Locs(2,k)
r2(i)=webread('https:/........=B(i), C(i)');
end
In the above code I want to extract each value in Locs matrix and for each pair of value i.e., B and C I want to automatically calculate r2(i) which is basically calling a webside. When I code it like this to automatically use the values of B(i) and C(i) it gives me below mentioned error. But when I input values manually and calculate it works fine. I think there is an issue with the way I am giving B(i) and C(i) in the website. Please correct me.
Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 373)
The server returned the status 400 with message "Bad Request" in response to the request to
URL https:.....
Error in readContentFromWebService (line 46)
byteArray = copyContentToByteArray(connection);
Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);

 採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 18 日

0 投票

r2(k) = webread( "https:/........=" + B(k) + ", " + C(k));
assuming that the syntax is something followed by a literal equal sign, followed by the content of B(k), followed by a comma and then space, then the content of C(k)
Caution: it is uncommon for spaces to appear in URLs. When they do appear, they often have to be encoded as %20

6 件のコメント

Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022 年 3 月 18 日
編集済み: Muhammad Qaisar Fahim 2022 年 3 月 18 日
No its Like this
webread('https://abc.xyz.org/a1/mem21m?sites=30, -40')
It is actually an api to calculate some thing online where these 30 and 40 are the inputs which can be changed. I actually want to change these inputs 100 times automatically
Walter Roberson
Walter Roberson 2022 年 3 月 18 日
What problem are you observing with the code I gave?
baseurl = "https://abc.xyz.org/a1/mem21m?sites="
baseurl = "https://abc.xyz.org/a1/mem21m?sites="
B = randi([-90 90], 1, 5)
B = 1×5
-38 -36 62 -83 -52
C = randi([-90 90], 1, 5)
C = 1×5
82 71 59 -30 -17
for k = 1 : length(B)
thisurl = baseurl + B(k) + ", " + C(k)
end
thisurl = "https://abc.xyz.org/a1/mem21m?sites=-38, 82"
thisurl = "https://abc.xyz.org/a1/mem21m?sites=-36, 71"
thisurl = "https://abc.xyz.org/a1/mem21m?sites=62, 59"
thisurl = "https://abc.xyz.org/a1/mem21m?sites=-83, -30"
thisurl = "https://abc.xyz.org/a1/mem21m?sites=-52, -17"
... I still caution that you might need to use %20 instead of space.
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022 年 3 月 18 日
編集済み: Muhammad Qaisar Fahim 2022 年 3 月 18 日
I am unable to understand one point here. Why you are writing it in double qoutataion and why are you not usingweb read? As the purpose of this web link is to calculate something from website not just populate the address
Steven Lord
Steven Lord 2022 年 3 月 18 日
Double quotes makes a string array as opposed to single quotes which makes a character array. The + operator for string arrays is concatenation; for character arrays it converts the characters to numeric codes and then performs addition.
S = "abc"
S = "abc"
C = 'abc'
C = 'abc'
S + 1 % "abc1"
ans = "abc1"
C + 1 % [98 99 100], the codes of the 'a', 'b', and 'c' characters plus 1
ans = 1×3
98 99 100
Walter didn't use webread because the point of the example was to show how to construct the addresses. You've already shown you are familiar with how to call webread, so what you needed assistance with was the text manipulation.
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022 年 3 月 18 日
Thanks yes this helps. Very nice explanation
Walter Roberson
Walter Roberson 2022 年 3 月 18 日
baseurl = "https://abc.xyz.org/a1/mem21m?sites="
baseurl = "https://abc.xyz.org/a1/mem21m?sites="
B = randi([-90 90], 1, 5)
B = 1×5
85 84 -10 -89 13
C = randi([-90 90], 1, 5)
C = 1×5
-22 65 -9 -84 40
for k = 1 : length(B)
thisurl = baseurl + B(k) + ", " + C(k)
result{k} = webread(thisurl)
end
thisurl = "https://abc.xyz.org/a1/mem21m?sites=85, -22"
Error using webread
Could not access server. https://abc.xyz.org/a1/mem21m?sites=85,+-22.
And that is why I did not use webread(): I knew it would generate an error on the first attempt, so you would have seen only one example URL (because the error would have prevented the other example URL from being constructed.) As Steven indicates, the problem you were having trouble with was the text manipulation.

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by