MATLAB Answers

running live functions and having section breaks

조회 수: 38(최근 30일)
Roy Goodman
Roy Goodman 2021년 2월 22일
댓글: Roy Goodman 2021년 4월 1일
Before the advent of Live Script, I created a lot of demonstrations using publish and I would always create the demonstrations as functions rather than as simple scripts. This was to avoid having to use clear and close all at the beginning of the program, since entering the function allows you to create variables inside the function's own scope and publishing the code instead of running it the regular way uses entirely new figure windows.
Now I use Live Scripts to do the same thing. If the code is defined with a function command at the top line, then the "Run" button disappears from the editor. I also lose the ability to add section breaks.
My guess is the designers of MATLAB have a good reason for this, but I really miss it. Are there any workarounds?
I suppose this boils down to one question:
Is there a way, inside a live script, to use a variable scope other than the base workspace?

채택된 답변

Aakash Mehta
Aakash Mehta 2021년 3월 31일
To add the functions in the live script, include at least one line of script code before the local functions. Add all local functions at end of the file.
For more details refer to the following MATLAB answer.
Regarding the variable scope, local functions like all other functions have their own workspaces that are separate from the base workspace. That is also applicable for the functions in the live scripts.
For more details regarding workspaces in the script, refer to the following link.
  댓글 수: 1
Roy Goodman
Roy Goodman 2021년 4월 1일
Functions defined in m-files can be published using "Publishing Options" and can be divided into sections using the double percent sign (%%). I don't see any reason why the Mathworks shouldn't be able to add Publishing Options to the Live editor as well, and thus allow the functions to be divided into sections.
I understand that MATLAB can't do this right now, but it seems absolutely feasible given that they have implemented the same functionality in m-files.

댓글을 달려면 로그인하십시오.

추가 답변(0개)





Community Treasure Hunt

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

Start Hunting!

Translated by