Why I cannot define a function in live script?
Show older comments
Hi,
I tried to define a function in LiveScript but as soon as I add it to a script it freezes the "Run" button.
However, while writing normal script that NOT happened.
Does anyone have an answer for that? I will appreciate it.
Accepted Answer
More Answers (1)
Hi,
you are right - it is freezed:

But there is nothing wrong with this, since you can not run a function which is defined but not called. If you call this function like this:

you are able to run it properly.
There is a similar behavior with the "normal" script files. The Run button is not disabled, but you will receive an error message:

which doesnt really help. If you again call the function properly, the result is the same:

So in both cases you have the same result, except that the Live Editor is a bit smarter because it recognizes that this feature can not be run alone without a call.
You can work around this behavior by combining a function and calling the function code in a live script. But then you have to pay attention to the naming, as the function and the .mlx file can no longer have the same name. Also the function definitions have to be at the end of the script:

As learned from Madhans answer, there is a section break made automatically, since you inserted a function.
Also for the classic script:

Best regards
Stephan
1 Comment
chemeng100
on 14 Jan 2019
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!