Hi, I would like to do a function that I can use many times in my code, like if I code in C language. I want to say that my function that 2 arguments (2 int for example, event if there is no type in matlab). Is it possible ? Thank you

1 回表示 (過去 30 日間)
RogerLM
RogerLM 2016 年 4 月 27 日
回答済み: Jan 2016 年 4 月 27 日
for example I code : function Y = moncode(X, Y) I would like to use this function many times in my code but my variable won't be everytime X, and Y ...
  1 件のコメント
Stephen23
Stephen23 2016 年 4 月 27 日
編集済み: Stephen23 2016 年 4 月 27 日
The documentation explains quite clearly how to create functions in MATLAB:
MATLAB has very readable and accessible documentation: beginners do themselves a big favor when they to learn to search and read it.

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

回答 (2 件)

Steven Lord
Steven Lord 2016 年 4 月 27 日
That's fine. The variable names used inside a function don't need to agree with the names of the variables passed into the function as inputs; in fact, the inputs to the function don't even need to be named variables!
abracadabra = 37;
why(abracadabra) % This works
why(37) % This works too
Ideally, users of a function shouldn't know or care what the developer of the function wrote inside the function, and that includes variable name choice. You may be curious, but you shouldn't NEED to know to use the function.
Is the name of the variable that contains the input value inside why.m named abracadabra? No.
Does that matter to why? No.

Jan
Jan 2016 年 4 月 27 日
User-defined functions follow the same rules as built-in Matlab functions. See:
sin(2.1)
X = 2.1
sin(X)
Y = X;
sin(Y)
The sin function does not care about the name of the variable, because it processes the value only.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by