Why I cannot define a function in live script?
39 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
chemeng100
el 14 de En. de 2019
Comentada: adams13
el 22 de Abr. de 2021
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.
0 comentarios
Respuesta aceptada
madhan ravi
el 14 de En. de 2019
Editada: madhan ravi
el 14 de En. de 2019
It clearly mentioned in the documentation below:
https://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html#bvclt99 - make sure the function is at the end if the file and the codes above it.
6 comentarios
Axel Jacobsen
el 16 de En. de 2021
I will third this; coming from Jupyter Notebook, I was hoping for a great usability improvement from Live Scripts (compared to normal Matlab) - however, this, along with live scripts not having native animation ability is quite suprising. Hopefully LiveScripts improves quickly in the future.
adams13
el 22 de Abr. de 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)');
end
% This is a part I want to improve
res = a + b;
end
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.
Más respuestas (1)
Stephan
el 14 de En. de 2019
Editada: Stephan
el 14 de En. de 2019
Hi,
you are right - it is freezed:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200711/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200712/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200713/image.png)
which doesnt really help. If you again call the function properly, the result is the same:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200714/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200715/image.png)
As learned from Madhans answer, there is a section break made automatically, since you inserted a function.
Also for the classic script:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200716/image.png)
Best regards
Stephan
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!