Using Matlab App Designer, error using drop down list. Incompatible array sizes.

Im atttempting to use a drop down list to select a country, which will pull information from a website and populate an Edit Field(text). The code works for the first two countries, when I use the third I get an incompatible array size error.
data = webread('https://restcountries.com/v3.1/all');
data = webread('https://restcountries.com/v3.1/all');
app.Country_Select.Value = 'Niger';
if app.Country_Select.Value == 'Jordan'
app.SpokenLanguageEditField.Value = data{1,1}.languages;
elseif app.Country_Select.Value == 'Brazil'
app.SpokenLanguageEditField.Value = data{2,1}.languages;
else app.Country_Select.Value == 'Niger'
app.SpokenLanguageEditField.Value = data{9,1}.languages;
end
Arrays have incompatible sizes for this operation.
app.SpokenLanguageEditField.Value

 채택된 답변

Instead of using == to compare character vectors, use strcmp.
val = app.Country_Select.Value;
if strcmp(val,'Jordan')
idx = 1;
elseif strcmp(val,'Brazil')
idx = 2;
else
idx = 9;
end
app.SpokenLanguageEditField.Value = data{idx,1}.languages;
== compares element-by-element, i.e., character-by-character in this case since you're comparing character vectors, so the number of elements must be the same, i.e., the character vectors must be the same length. If the character vectors are not the same length, as in 'Niger' == 'Jordan', you'll get the error you got.
You could also use switch/case.
val = app.Country_Select.Value;
switch val
case 'Jordan'
idx = 1;
case 'Brazil'
idx = 2;
otherwise
idx = 9;
end
app.SpokenLanguageEditField.Value = data{idx,1}.languages;

댓글 수: 5

I'll try using Switch, thank you!
You're welcome! Any questions, let me know. Otherwise, if this answer works, please "Accept This Answer". Thanks!
I'm attempting to use the Switch command now in App Designer, and I am recieving an error code that the 'Value' must be a character or string scalar. When running the code in a script it displays the result as a 'struct'. I'm quite new to Matlab, do I need to convert this to a character vecotr?
I believe I figured it out, thank you again for your help and rapid response!
You're welcome! Glad you got it working!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 7월 17일

댓글:

2023년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by