How to pass 3 variables from a script into a function and get 1 output?

조회 수: 3 (최근 30일)
Akana Juliet
Akana Juliet 2021년 6월 30일
댓글: Rik 2021년 6월 30일
Hey everyone, I am trying to call a function from a script that basically gives 3 variables and returns 1 result. My function ("GetResult") is already written, it asks which mode (1-6), then goes into an if statement and asks if the user wants a random generated result or a fixed result. if isRand == 1, then random result is generated. if isRand == 2, then they are prompted to enter the Index number, then result is given.
This is all I have so far for an idea on how to call it from another function.
result = GetResult(Mode, isRand, Index);
So my main question is, how can I pull this off? I feel like I need to assign certain numbers to words, Like I can't use 1-6 for Mode, Since this script I am calling it from will be eventually attached to the GUI. I need to say which each mode is. Is this the best way to do this? I was thinking about putting this statment below in my GetResult function. What do you think? Thank you in advance!
if Mode == "AB"
GetResult(1)
elseif Mode == "CD" || "EF" || "GH"
GetResult(2)
elseif Mode == "IJ"
GetResult(3)
elseif Mode == "KL"
GetResult(4)
end

채택된 답변

Rik
Rik 2021년 6월 30일
Where are the letters coming from?
Anyway, if you want to do something like this, you should use strcmp to compare char arrays. A better solution is probably to use switch:
switch Mode
case "AB"
Mode=1;
case {"CD","EF","GH"}
Mode=2;
case "IJ"
Mode=3;
case "KL"
Mode=4;
otherwise
error('invalid Mode selected')
end
output=GetResult(Mode);
  댓글 수: 2
Akana Juliet
Akana Juliet 2021년 6월 30일
Thank you! This makes sense. When you say:
output=GetResult(Mode);
could I still use:
result = GetResult(Mode, isRand, Index);
and get my single result back?
Rik
Rik 2021년 6월 30일
Absolutely. I would even say that is the preferred way to do it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by