Hi, I want a solution to this problem. Someone can help me?
Error in operations (line 5)
elseif c == 'multi'

 채택된 답변

Star Strider
Star Strider 2015년 5월 30일

6 개 추천

I have no idea what the matrix dimension problem is, since we have only one line of your code. However, the way to compare strings and string variables is to use the strcmp or strcmpi functions.
So the correct syntax for the line you posted would be:
elseif strcmpi(c, 'multi')

댓글 수: 7

Alvaro Arenas
Alvaro Arenas 2015년 5월 30일
편집: Star Strider 2015년 5월 30일
Hi, Star Strider. Thank you for answer my question
I corrected the matlab code but now I have this problem
>> o1 = operations(4,'suma')
o1 =
8
>> o2 = operations(4,'multi')
Error in operations (line 3)
if strcmpi(c, 'suma')
Output argument "o1" (and maybe others) not assigned during call to
The logic in your code is correct. I experimented with all the options.
You can realistically have only one output, not three, so I would change your code to:
function [out] = operations(M,c)
out = NaN;
if strcmpi(c, 'suma')
out = M+M;
elseif strcmpi(c, 'multi')
out = M*M;
elseif strcmpi(c, 'expo')
out = M^M;
end
end
That should eliminate the error.
The initial assignment setting out to NaN covers the error condition when nothing matches ‘c’ correctly. The operation would not be defined, so that would actually be appropriate.
Star Strider Thank you so much. I resolved this problem
Star Strider
Star Strider 2015년 5월 31일
My pleasure.
If my Answer helped you resolve your problem, please Accept it.
KenoKanawa
KenoKanawa 2018년 11월 16일
Thank you, it solved my problem too !
Star Strider
Star Strider 2018년 11월 16일
@KenoKanawa — My pleasure!
Dunzhi Chen
Dunzhi Chen 2020년 6월 25일
Thanks, this solved my problem!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pronoy Das
Pronoy Das 2021년 2월 25일
편집: Pronoy Das 2021년 2월 25일

2 개 추천

I came across a similar problem. Simply replace the single quotation marks with the double quotation marks, i.e. replace the line in your code with
c == "multi"

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 5월 30일

편집:

2021년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by