error size dimension must match

1 vue (au cours des 30 derniers jours)
rizqi multazam
rizqi multazam le 30 Oct 2021
hi everyone,im sorry im new on matlab and i got school project,i use if else for plot and i dont know why my code cant run the 1st elseif,its always pop up error message related documentation,thanku for advance,sorry for bad english
clc
clear
a = input('\n testa : ', 's');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
b = input('\n testb : ', 's');
c = 'testc';
d = 'testd';
e = 'a'
if a==c , b==d
x1 = [2 3 3.7 5 4.9];
y1 = [2 2 2.1 2.1 -4];
plot(x1,y1,'g')
title('Combine Plots')
hold on
x2 = [2 1 1 1.2 2.1 3.5 4.9];
y2 = [2 1.9 -2.6 -4.2 -3.9 -3.8 -4.1];
plot(x2,y2,'r')
hold off
text(1.7,1.7,'MIT');
text(5.1,-4.2,'AGD');
DS = text(2.5,2.3,'ACD');
set(DS,'Rotation',2);
DG = text(5.3,0,'AFQ');
set(DG,'Rotation',-91);
SW1 = text(0.7,-2.1,'ARD');
set(SW1,'Rotation',90);
SW2 = text(0,2.1,'AOS');
set(SW2,'Rotation',3);
GN = text(2.2,-4.2,'GAS');
text(1.2,3,'1.1')
fprintf('1.1 km\n')
elseif (a==c) , (b==d)
x1 = [2 3 3.7 5 4.9];
y1 = [2 2 2.1 2.1 -4];
plot(x1,y1,'g')
title('Combine Plots')
hold on
x2 = [2 1 1 1.2 2.1 3.5 4.9];
y2 = [2 1.9 -2.6 -4.2 -3.9 -3.8 -4.1];
plot(x2,y2,'r')
hold off
text(1.7,1.7,'MIT');
text(5.1,-4.2,'AGD');
DS = text(2.5,2.3,'ACD');
set(DS,'Rotation',2);
DG = text(5.3,0,'AFQ');
set(DG,'Rotation',-91);
SW1 = text(0.7,-2.1,'ARD');
set(SW1,'Rotation',90);
SW2 = text(0,2.1,'AOS');
set(SW2,'Rotation',3);
GN = text(2.2,-4.2,'GAS');
text(1.2,3,'1.1')
fprintf('1.1 km\n')
end

Réponses (1)

Walter Roberson
Walter Roberson le 30 Oct 2021
a = input('\n testa : ', 's');
b = input('\n testb : ', 's');
c = 'testc';
d = 'testd';
e = 'a'
character vectors.
if a==c , b==d
In MATLAB, that is the same as
if a==c
b==d
which is to say that a==c is computed, and if if determines that the condition passes, then the body of the if statement would be entered. The body of the if statement would then compute b==d as an expression, and output the results of the expression, because there is no semi-colon to surpress the output.
MATLAB does not treat that as a compound if statement. It is not testing that a==c and b==d . If you want to test multiple conditions then you need to join them into one expression using the & or && or | or || operators as appropriate.
Now let us look more closely at the a==c part. Character vectors such as you get back from input() with the 's' option, or such as you get using apostrophes to quote character such as 'testc': those are literally vectors of character values. Vectors, as in containing multiple values. Just like testing [65 71 68] == [65 71 53 68], the == operator compares corresponding vector positions -- and gives an error if the vectors are different length. [65 71 68] == [65 71 53 68] does not test to see if the two numeric vectors have the same length and the same contents, and does not test whether [65 71 68] is a leading prefix of [65 71 53 68]. Just so, 'AGD' == 'AG5D' does not test to see whether the character vectors have the same length and same contents, and does not test whether 'AGD' is a leading prefix of 'AG5D' .
If you want to compare two character vectors of possibly different length for equality, use strcmp() . If you want to test two character vectors of possibly different strings to see if they are the same except perhaps for upper/lower case ('Hello' == 'hello' ?) then use strcmpi()

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by