MATLAB Answers

Why I cannot define a function in live script?

390 views (last 30 days)
chemeng100 on 14 Jan 2019
Commented: adams13 on 22 Apr 2021
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

madhan ravi
madhan ravi on 14 Jan 2019
Edited: madhan ravi on 14 Jan 2019
It clearly mentioned in the documentation below: - make sure the function is at the end if the file and the codes above it.
adams13 on 22 Apr 2021
It is very useful functionality that MathWorks is trying to disable. It works in a normal script and for some reason it is disabled in a live script.
Consider you have some complex data processing within a 'main' script. Now you want to put some calculations in a separate file. You are going to run this file many times changing the code to make it finally do what you want. However, you do not want to run the 'main' script every time because it is time consuming. This way, you run the 'main' script once, it stores the data in the variables 'a' and 'b'. Afterwards, you can run the 'test' script just by F5, it will recognize that it was called without arguments and will take its arguments from the base workspace. When it is ready, you just run the 'main' script that calls this as a function 'x = test (a, b)'. In case of problems, you can easily come back and run this 'test' function "stand-alone".
function res = test(a, b)
if nargin == 0
[a, b] = evalin('base', 'deal(a, b)');
% This is a part I want to improve
res = a + b;
Unfortunately, this is disabled in a live script. Additional unfortunate decision by Mathworks: all the output from a live script function is placed just after the call and not 'in place'. Effectively, 'live script' functionality is disabled within a function, so you can forget about live scripts and use just a normal function.

Sign in to comment.

More Answers (1)

Stephan on 14 Jan 2019
Edited: Stephan on 14 Jan 2019
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
  1 Comment
chemeng100 on 14 Jan 2019
Thank you Stephan for showing the examples. It´s very helpful.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!