Compare strings and print to the screen

2 ビュー (過去 30 日間)
Nicle Davidson
Nicle Davidson 2021 年 9 月 4 日
回答済み: Chunru 2021 年 9 月 4 日
I would like to compare to strings and if they are alike write out a number to the scrren like this:
A='thisisastr';
B='anotherstr';
C='anothernot';
aA='anothernot';
bB='thisisastr';
cC='anotherstr';
QQ='something else';
if strcmp(aA,A)
W=1;
elseif strcmp(aA,B)
W=2;
elseif isequal(aA, C)
W=3;
end
if strcmp(bB,A)
U=1;
elseif strcmp(bB,B)
U=2;
elseif strcmp(bB,C)
U=3;
end
if strcmp(cC,A)
Z=1;
elseif strcmp(cC,B)
Z=2;
elseif strcmp(cC,C)
Z=3;
end
Then I would like to write it to the screen:
fprintf('The house is:%d%d%d%s',W, U, Z, QQ);
But I get this error on fprintf:
Unrecognized function or variable 'W'.
Does anyone see where is the mistake please

採用された回答

Chunru
Chunru 2021 年 9 月 4 日
The code works:
A='thisisastr'; B='anotherstr'; C='anothernot';
aA='anothernot'; bB='thisisastr'; cC='anotherstr';
QQ='something else';
if strcmp(aA,A)
W=1;
elseif strcmp(aA,B)
W=2;
elseif isequal(aA, C)
W=3;
end
if strcmp(bB,A)
U=1;
elseif strcmp(bB,B)
U=2;
elseif strcmp(bB,C)
U=3;
end
if strcmp(cC,A)
Z=1;
elseif strcmp(cC,B)
Z=2;
elseif strcmp(cC,C)
Z=3;
end
fprintf('The house is: %d%d%d %s',W, U, Z, QQ);
The house is: 312 something else

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by