Calling a user-created function in a different script

Hello everyone,
So I created a function that resets some variables in a game, once the game has finished. I want to call that function in a script, their both in the same folder and are both accessible by matlab. I come more from C++ so I am used to an explicit #include "", but I know that is not how matlab does things. So I am wondering how one can call a function (the function header has the same name as the function's .m file) in a script. Thank you in advance.

 採用された回答

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 22 日
編集済み: Setsuna Yuuki. 2020 年 11 月 22 日

1 投票

You can call a function in your folder
for example if your function is:
function x = mulx2(y)
x = 2*y;
end
from the main file you should call it like this:
number = 2;
var = mulx2(number)
ans
var = 4
The name of the function and the "file.m" are the same, in this case if your function is mulx2, your file should be mulx2.m

6 件のコメント

Mark Terrero
Mark Terrero 2020 年 11 月 22 日
編集済み: Mark Terrero 2020 年 11 月 22 日
So what you're saying is I can call my function Reset() from my main file, meaning the script? Because if so, maybe I messed something up, although its a pretty straight forward process, but I did do that initially.
Mark Terrero
Mark Terrero 2020 年 11 月 22 日
編集済み: Mark Terrero 2020 年 11 月 22 日
If by main file you mean the command window, that would defeat the purpose of what I am trying to do, I am trying to have it reset (to initial state) variables when a person wins the game.
Mark Terrero
Mark Terrero 2020 年 11 月 22 日
Nevermind
Stephen23
Stephen23 2020 年 11 月 22 日
編集済み: Stephen23 2020 年 11 月 22 日
"... I am trying to have it clear all variables when a person wins the game."
MATLAB is a high-level language which takes care of memory management for you: it is very unlikely that you need to explicitly clear any variables. The best way to keep your workspace/s tidy is to use functions rather than scripts.
Mark Terrero
Mark Terrero 2020 年 11 月 22 日
編集済み: Mark Terrero 2020 年 11 月 22 日
I should have been slightly more literal, I am not clearing the variables I am resetting them to their initial state. Going back and fixing anytime I say "clear all" to "reset"
Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 22 日
Main file can be command window or other script.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2020 年 11 月 22 日

編集済み:

2020 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by