Reputation: 339
I recently started using Live editor in MATLAB and I inserted a function inside it. But, apparently, I cannot execute that particular section of code where I type function. Even the section break disappears. Is it that using function is not suitable for live editor?
Upvotes: 1
Views: 1835
Reputation: 46530
Apparently the MATLAB parser did not join the 20th century until partway through 2016, and could not interpret function definitions in scripts (live or otherwise) until R2016b. In the web-based docs, there is a notice at Add Functions to Scripts, but it took me a while to find this out because the builtin docs in R2016a or earlier do not explicitly contain this information. It is implied by the tutorials that tell you to create a new file for each function (which to me, a python programmer, sounds more like strange advice than a restriction).
Trying to define a function in a live script gives confusing errors. For example, if you create a cell with this content:
function y = myfunc(x)
y = 2*x;
end
It will underline the keyword function
with a popup error that reads:
Parse error at FUNCTION: usage might be invalid MATLAB syntax.
Might be? Whom shall I ask? Upon running the cell, it prints an error after the first line:
All functions in a script must be closed with an
end
.
I eventually made this discovery myself thanks to a helpful message if the first thing you happen to do in a new empty live script is to start typing function
on the first line; as soon as you hit the spacebar a message pops up at the top saying:
Functions and classes are not supported in the Live Editor. To continue, save the file as a plain text code file (
.m
).
Upvotes: 1
Reputation: 326
It should work as when you add a function inside a script. For example, like this:
What function are you exactly trying to code?
Upvotes: 0