Function inside script in Matlab?

10,207

This is correct, MATLAB does not allow you to define full functions in a script. However, there are at least two solutions that may help you:

  1. You could turn the script into a function. Workspace variables you are referring to from within your scripts would become arguments of the function, and you could return certain result variables.

  2. If your helper function is really small, you may be able to define it in your script as an anonymous functions without the function keyword, as in poly = @(x) x.^2 + 3 * x - 4; - a polynomial function, for example.

Share:
10,207

Related videos on Youtube

Suzan Cioc
Author by

Suzan Cioc

Not to be offended

Updated on September 14, 2022

Comments

  • Suzan Cioc
    Suzan Cioc over 1 year

    Can I have fast and short helper local function to use inside script?

    Currently I have "FUNCTION keyword use is invalid here" message.

    Why?