Calling a user-created function in a different script

2 Ansichten (letzte 30 Tage)
Mark Terrero
Mark Terrero am 22 Nov. 2020
Bearbeitet: Stephen23 am 22 Nov. 2020
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.

Akzeptierte Antwort

Setsuna Yuuki.
Setsuna Yuuki. am 22 Nov. 2020
Bearbeitet: Setsuna Yuuki. am 22 Nov. 2020
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 Kommentare
Mark Terrero
Mark Terrero am 22 Nov. 2020
Bearbeitet: Mark Terrero am 22 Nov. 2020
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. am 22 Nov. 2020
Main file can be command window or other script.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Number games finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by