필터 지우기
필터 지우기

Help with error arrays have incompatible sizes

조회 수: 34 (최근 30일)
Laith
Laith 2023년 11월 20일
편집: DGM 2023년 11월 20일
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?

답변 (2개)

DGM
DGM 2023년 11월 20일
편집: DGM 2023년 11월 20일
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
Walter Roberson
Walter Roberson 2023년 11월 20일
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
DGM 2023년 11월 20일
편집: DGM 2023년 11월 20일
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
Walter Roberson 2023년 11월 20일
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 = 'rare'
s1(1), s1(2), s1(3), s1(4)
ans = 'r'
ans = 'a'
ans = 'r'
ans = 'e'
double(s1)
ans = 1×4
114 97 114 101
s2 = ['r' 'a' 'r' 'e']
s2 = 'rare'
s2(1), s2(2), s2(3), s2(4)
ans = 'r'
ans = 'a'
ans = 'r'
ans = 'e'
double(s2)
ans = 1×4
114 97 114 101
%Observe that 'rare' and ['r' 'a' 'r' 'e'] are exactly the same outcome
s3 = 'well'
s3 = 'well'
s3(1), s3(2), s3(3), s3(4)
ans = 'w'
ans = 'e'
ans = 'l'
ans = 'l'
double(s3)
ans = 1×4
119 101 108 108
s4 = 'med'
s4 = 'med'
s4(1), s4(2), s4(3)
ans = 'm'
ans = 'e'
ans = 'd'
double(s4)
ans = 1×3
109 101 100
whos s1 s2 s3 s4
Name Size Bytes Class Attributes s1 1x4 8 char s2 1x4 8 char s3 1x4 8 char s4 1x3 6 char
s1 == s2 %valid because they are the same size
ans = 1×4 logical array
1 1 1 1
s1 == s3 %valid because they are the same size
ans = 1×4 logical array
0 0 0 0
s1 == s4 %error because they are different sizes
Arrays have incompatible sizes for this operation.
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.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by