function in a script file

3 Ansichten (letzte 30 Tage)
PA
PA am 1 Sep. 2022
Kommentiert: Walter Roberson am 2 Sep. 2022
Can I write a function inside a if statement in a script file?
  1 Kommentar
Walter Roberson
Walter Roberson am 2 Sep. 2022
@PA if you are not able to understand the responses, then please ask for clarification... after restoring the code, so that people will understand the situation you are working with.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 1 Sep. 2022
"function" statements can never be inside a flow control statement.
Anonymous functions can be defined inside flow control.

Weitere Antworten (2)

David Hill
David Hill am 1 Sep. 2022
a=5;b=10;
if b>a
c=executeFunction(a,b)
end
c = 35
function c=executeFunction(a,b)
c=b*3+a;
end
  3 Kommentare
PA
PA am 1 Sep. 2022
yeah its written for class method
Steven Lord
Steven Lord am 1 Sep. 2022
If obj is an instance of a class with a method named CON_Pattern you could call the method as obj.CON_Pattern or as CON_Pattern(obj). But you would have to define it as function CON_Pattern(obj).

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 1 Sep. 2022
You cannot define a function inside an if statement.
You can define a function in a script or function file outside of the if statement and call it inside.

Kategorien

Mehr zu Modeling 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