how to use variable from function ?

1 回表示 (過去 30 日間)
tomer polsky
tomer polsky 2017 年 10 月 21 日
コメント済み: KL 2017 年 10 月 21 日
hello i dont understand how to use variable from function here is exmaple :
function []=my_fun(x)
a=22
end
b=a+2
i want to use a away from the function how to do it ?

回答 (1 件)

KL
KL 2017 年 10 月 21 日
A function has inputs and outputs, for example
function a =my_fun(x)
a=22+x;
end
Here you are getting 'a' as the output of your function while 'x' is your input. Inside this function you see, you can use 'x'.
Now if you store this as my_fun.m in your directory, on the command line you can use
a = my_fun(2)
then you will get the output like
a = 24
since inside the function we add 22 to the input of the function.
  2 件のコメント
tomer polsky
tomer polsky 2017 年 10 月 21 日
i am sorry but I did not understand, I want to get variable a out of the function how to do it ?
KL
KL 2017 年 10 月 21 日
That's exactly what I wrote there!
function a =my_fun(x)
In this line, a is the output of the function.

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

カテゴリ

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