Help with error arrays have incompatible sizes
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
My error is simple but i dont know how to fix it.
In app designer, i created a button that shows information when clicked but i get the error "arrays have incompatible sizes" how do i fix this?
0 comentarios
Respuestas (2)
DGM
el 20 de Nov. de 2023
Editada: DGM
el 20 de Nov. de 2023
Don't use == to do string/char comparison. Use strcmp/strcmpi, or better yet, just replace the whole if/elseif chain with a switch/case structure
% instead of
if strcmpi(fruitname,'apple')
disp('I like apples') % some example action
else strcmpi(fruitname,'orange')
disp('Orange juice is great')
else strcmpi(fruitname,'pineapple')
disp('Pineapple is okay')
else
fprintf('I don''t know what a %s is',fruitname)
end
I think it's easier and clearer to use a switch-case for this
% just do
switch lower(fruitname)
case 'apple'
disp('I like apples')
case 'orange'
disp('Orange juice is great')
case {'pineapple','ananas'} % this makes it easier to group cases if needed
disp('Pineapple is okay')
otherwise
fprintf('I don''t know what a %s is',fruitname)
end
3 comentarios
Walter Roberson
el 20 de Nov. de 2023
None of us are going to type in all that code from a picture of the code, just so we can show you the exact revised code.
DGM has shown you two strategies to deal with the situation: either use strcmp() or strcmpi() or else use switch / case. If that is not enough for you to understand what needs to change, then post code for us to edit.
DGM
el 20 de Nov. de 2023
Editada: DGM
el 20 de Nov. de 2023
There isn't anything special about appdesigner code here. You're taking a string/char and comparing it to some reference. There are other ways this can be done via array indexing, but to extend the above examples:
function PrintInfoButtonPushed(app,event)
switch lower(app.SelectionTextArea.Value)
case 'india'
% ...
case 'finland'
% ...
otherwise
% ...
end
end
Walter Roberson
el 20 de Nov. de 2023
MATLAB has two closely related ways of representing characters.
The older, and still valid way, is as an array of char . char arrays are internally stored as uint16 (unsigned 16 bit integers) but with a flag indicating they are to be considered printable. As such, they have all the standard properties of numeric arrays -- including the property that if you use == to compare two numeric arrays, that it is an error if the arrays are not the same size.
s1 = 'rare'
s1(1), s1(2), s1(3), s1(4)
double(s1)
s2 = ['r' 'a' 'r' 'e']
s2(1), s2(2), s2(3), s2(4)
double(s2)
%Observe that 'rare' and ['r' 'a' 'r' 'e'] are exactly the same outcome
s3 = 'well'
s3(1), s3(2), s3(3), s3(4)
double(s3)
s4 = 'med'
s4(1), s4(2), s4(3)
double(s4)
whos s1 s2 s3 s4
s1 == s2 %valid because they are the same size
s1 == s3 %valid because they are the same size
s1 == s4 %error because they are different sizes
The error is not that the two arrays have different content -- if it were then s1 == s3 would fail. The error is that s1 and s4 are different sizes. The == operator requires that the things being compared are the same size (after any implicit expansion)
The value you are getting out of the ui control is a character vector. You are using == to compare that character vector to 'India' which is itself a character vector. If what is stored in the ui control just happens to be exactly the same length as 'India' then the == operator will not error -- but if what is stored in the ui control is a different length, then the == operator will error.
The key workaround when using char vectors is to use strcmp() or strcmpi() to compare character vectors. strcmp() and strcmpi() start by testing the sizes of the parameters, and if they are different sizes it immediately returns false -- without even bothering to look at the characters stored in the parameters. It only proceeds to check whether they are the same characters if the sizes are exactly equal.
The second way that MATLAB represents characters is to store them in string() objects. Each entry in a string object is a character vector considered as a group . There is a == operator defined for string() objects, and that operator knows to check sizes before anything else.
string() objects in MATLAB are indicated by using " " instead of ' ' . So for example, s5 = "med" would be considered to be a 1 x 1 array in which the first entry, s5(1), is the group "med" . And you can do s1 == s5 and the == operator will notice that s5 is a string() object and will convert s1 to a string() object too and then compare "rare" == "med" which would not be an error because == for strings() knows about checking sizes.
0 comentarios
Ver también
Categorías
Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!