Can I write a function inside a if statement in a script file?

1 commentaire

Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 1 Sep 2022

0 votes

"function" statements can never be inside a flow control statement.
Anonymous functions can be defined inside flow control.

Plus de réponses (2)

David Hill
David Hill le 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 commentaires

Walter Roberson
Walter Roberson le 1 Sep 2022
Modifié(e) : Walter Roberson le 2 Sep 2022
function input = obj.CON_Pattern
The only time that you can use a period as part of a function name for a function definition, is for set and get methods of a classdef
You asked about script files but your code appears to have been written for class methods? (App Designer creates classes, not scripts)
PA
PA le 1 Sep 2022
yeah its written for class method
Steven Lord
Steven Lord le 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).

Connectez-vous pour commenter.

Steven Lord
Steven Lord le 1 Sep 2022

0 votes

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.

Catégories

En savoir plus sur Debugging and Analysis dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by