0

Matlab r2016b allows for functions to be embedded in scripts. I am trying to run this script, but an error occurs, and I do not know what rules I am breaking. Here is the script:

clear
clc

out=nestedvol(2) 

function outvol=nestedvol(l)
outvol=2*l
end

x=4   %this line does not work.

This question continues off of In MATLAB, can I have a script and a function definition in the same file?

How could I get a script to continue after the embedded function ends? Would an anonymous function be appropriate for this?

The reason I ask this is because I make review scripts for chapters of the matlab book I am reading, so it would be nice to section areas of the code off. It seems like if any function is not included, ctrl enter does not work.

Community
  • 1
  • 1
Dan Powers
  • 119
  • 8
  • 6
    Read the help: https://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html Local functions in scripts go at the end of the script, as indicated by the line that reads *You can add local functions in any order, as long as they all appear after the rest of the script code.* – nkjt Nov 01 '16 at 22:05
  • 2
    An anonymous function would, of course, work just fine. – beaker Nov 01 '16 at 22:11
  • @nkjt I checked that before, but missed the part you pointed out. – Dan Powers Nov 01 '16 at 23:06

0 Answers0