Doug's MATLAB Video Tutorials

April 27th, 2010

Converting from a formula to a script to a function

Very often the process of coding in MATLAB is to start from a requirement, like “implement this formula”, to a script that is easily tested to a function that is more general and adds to the MATLAB language. This process is followed in simplified for for temperature conversions.

4 Responses to “Converting from a formula to a script to a function”

  1. Claus replied on :

    I am an experienced but incompetent MATLAB user and really appreciate these simple tutorials. I didn’t learn anything new here but would like to hear more about functions within functions (e.g. what variables stay alive and which need to be passed on when one function calls another function)

  2. dhull replied on :

    @Claus,

    Thank you for the kind words.

    If I understand your question right, basically all variables need to be passed in and out of a function since they are destroyed once they leave scope of a function. Exceptions are made for persistent and global variables, but those are rare.

    Doug

  3. name replied on :

    Nice video.

    One related question – if I did want the value of “offset” to be stored in the workspace, but it is defined within the function code … how could I do that?

    I guess one idea would be to make it an output argument, or alternatively I think one could use the evalin function, but I just thought I would ask if there is some other easier way.

    Thanks.

  4. dhull replied on :

    Can you clarify your question? You want “offset” to be stored in which workspace (the temporary function workspace or the base workspace that call the function)

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.