Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Voss
Voss el 17 de Jul. de 2023
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 comentarios
Aaron
Aaron el 17 de Jul. de 2023
I believe I figured it out, thank you again for your help and rapid response!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by