switching output based on input

Hi, is it possible to switch the output of a function based on the input that was given to the function? im talking about switching to diffderent number and types of output?
thank you

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日

0 投票

You can use if else statement

6 件のコメント

Dany
Dany 2013 年 10 月 2 日
right, but when im defining the function i have to put an output in the definition. how would i change that output during the function runing?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
This is not clear. Please give an example
Dany
Dany 2013 年 10 月 2 日
lets say i have a function called "grid" that creats a grid from a gived geometry. if the input is a geometry of a square then the output will be [Xgrid, Ygrid], parameters created with the "meshgrid" function. on the other hand if the input is a geometry of a non rectengular shape then the output will be [p, e, t], parameters created by the "initmesh" function (triangular mesh).
i hope that was a batter explenation.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
Look at
help varargin
help varargout
help nargout
Dany
Dany 2013 年 10 月 2 日
ok, thank you.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
Example
function varargout=ans_switch(a)
if a==1
varargout{1}=10
varargout{2}=20
else
varargout{1}=100
end
% to call this function
% If a==1
[out1,out2]=ans_switch(1)
% If a~=1
out1=ans_switch(12)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDownloads についてさらに検索

製品

質問済み:

2013 年 10 月 2 日

コメント済み:

2013 年 10 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by