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.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
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)
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.
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.
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
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
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)
@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
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.
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