What is wrong with this easy script

1 visualizzazione (ultimi 30 giorni)
Joss E.
Joss E. il 12 Lug 2020
Commentato: Joss E. il 12 Lug 2020
in a new script,
Hello,
I am new in Matlab. I am practicing. I don´t know why, Matlab don´t accept this code:
function out=picker(condition,in1,in2)
if condition == 1
out=in1;
elseif condition == 0
out=in2;
end
What is wrong?
Thank you for you time
  2 Commenti
David Hill
David Hill il 12 Lug 2020
There is nothing wrong with it. How are you calling it? There is one thing that would simplify and always provide an output.
function out=picker(condition,in1,in2)
if condition %no need for ==1
out=in1;
else %no need for elseif
out=in2;
end
end
% should call function
a = picker(1,1,5) % need three inputs to the function
Joss E.
Joss E. il 12 Lug 2020
I understood it now. For me was very useful, thank you a lot.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 12 Lug 2020
You need to pass in something. You can't just click the green Run triangle without passing in anything for the arguments. For example, you could do this from the command line:
>>out = picker(true, 42, 73)
  1 Commento
Joss E.
Joss E. il 12 Lug 2020
For me was very useful. Thank you a lot for you time. You are very kind. Long life for you

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by