how does a function invoke the inputs?
Mostrar comentarios más antiguos
function [result] = test02(type,a,b,c,d)
%This function is used to test what will happen if only part of the inputs
%are needed to get the result.
switch type
case '1'
result=a+b;
case '2'
result=a+b+c+d;
otherwise
warning('Unexpected input type. Please check the input.')
end
end
I wonder how Matlab function tacitly 'uses' the inputs, so I wrote the function above and type the piece of codes below in the command line to see what if only part of the inputs are given. Unexpectedly, no error came up (Personally 'c' and 'd' are not essential to get the result for case '1'). Could anyone explain the reason why the function still works when only part of the inputs (type, a, b) are given? What's the mechanism of calling the inputs in a Matlab function?
type= '1';
a=1; b=2; c=3; d=4;
[result] = test02(type,a,b)
Respuesta aceptada
Más respuestas (1)
KSSV
el 6 de Mayo de 2020
0 votos
It is because, your type = '1' needs only two inputs a,b ..
You try using type = '2' with only two inputs..it will throw error. For the type = '1' case, it needs only two inputs a, b nd you have provided them so there is no error.
2 comentarios
Shuangfeng Jiang
el 6 de Mayo de 2020
Rik
el 6 de Mayo de 2020
You can put in anything you want, because your function is going to ignore the actual contents of the variable. Usually people use an empty array [] to show that an input is ignored.
type= '1';
a=1; c=3;
result = test02(type,a,[],c)
Categorías
Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!