Live Script Features for Online Teaching
From the series: Online Teaching with MATLAB and Simulink
Learn about the main features of live scripts applicable to teaching online while working with a simple example:
- Mix up code, text, and outputs in the same environment to create executable stories, such as a noisy signal
- Add richly formatted text, a table of contents, images, equations, and hyperlinks
- Edit code easily with auto-completion (parentheses, quotes, programming block ending), function name suggestion, and context-aware lists of optional parameters
- Use options to run section code and output visualization
- Edit plot annotations and automatic code upgrades interactively
- Refactor code with local functions
- Set breakpoints and debug step by step
- Use live controls to interact with the code, such as numeric slider to change noise parameter
- Use Live Editor tasks to speed up preprocessing tasks exploring different methods, like Smoothing Data task
- Export live scripts into shareable document formats such as PDF
In this video, I'm going to review the main features of Live Script. Click a New Live Script button, or use a new menu to open a new Live Script. You can also type edit and the file name with mlx extension. In a live script, you can mix up a text and code together. Enter code integrate areas and click on the text button to enter some text describing what you are going to do. Text is like a comment but without percentage.
Then click on the code button to enter more code. Note that after typing a few letters of the function name, it will pop up a list of the available functions starting with that prefix, and when you type the open parentheses, live editor will automatically complete it with a closed one. So we can collect a bunch of comments to create a whole code section, which is kind of computational unit, and then click on Run section button to immediately get the output.
This is a really useful workflow. You add more text to provide more background and explanation of the work in progress. And then you add a new lines of code, maybe to get a plot. As you can see MATHLAB will always assist you suggesting the correct syntax for any optional different parameter.
To re-execute the whole code, click on the Run section button again to see the graphics as well. Life's great is great to quickly experiment what happens when you change a parameter or what happens if we change the design in the function definition, or what happens if we add a square.
If we stumble in any syntax issue don't worry the error message can help you understand what is missing and correct. We could also add a breakpoint to debug the code and execute a step by step to catch also semantic errors. You can step to next line or continue to next breakpoint and when you are OK with the code you can stop debugging and eliminate all the breakpoints.
But just a note, when you save a script only MLX format allows to store output and text inside the file itself. A traditional script that would lose all the output and would turn texting to comment. Anyway any script can be converted into a live script simply by resaving it as MLX.
Now let me jump into a larger live script, to review how to format the text in different ways. For example, to create a title you click on the text and simply choose, Title, in the style menu. Or to create a heading you can either use the style menu, a shortcut or put a mark down before the text the, double sharp.
To add a section break, you can use either a section break button or the shortcut Control-Alt-Enter. If you put a double percentage before a text, you turn this into a heading with a new section breaks simultaneously. Of course, you can also create different levels of headings using the Start menu again.
All these headings will be used to conveniently to create a table of contents. In fact, if we just scroll up to the beginning, we can go to the Insert tab and click on the Table of Contents button, every item here in the table now, is a hyperlink to the corresponding section.
Now, suppose we want to tell a relevant story, let's say about a noisy signal, we'd better highlight the story with a new heading and check that any new heading is automatically added to the table of contents. But to create a good story, we need additional resources. For example, we could insert a hyperlink, pretty immediate, to copy the link. And this way you have a direct link from the script to a web page, maybe describing more statistics for random models.
What about adding a mathematical equations? Click Equation to access the equation addition. It provides many symbols like Greek letters, errors, operators, radicals, derivative, integrals, that can help write that equation. You can also use a Control Alt e shortcut to edit an equation. And if you are familiar with radar comment you can directly type radar comment. They all typically start with a backslash. So backslash mu backslash sigma will return to symbols for the mean and standard deviation.
And finally, to give us a visual idea of noise the signal we can insert an image. Let's choose our favorite JPEG. And here we can resize it as we like. And now we are ready to switch from text to code. To implement our story with a nice executable comments. We can start copying some code from the previous example.
Maybe taking a smaller step here. And we can click on the wrong section to execute and show the output. On the top right bar, we can click to visualize output airline after each comment if we like, or we can keep the default visualization of the output on the right. Let's go on with a custom plot. When you start writing the plot comment life's creator will help with additional parameters. For example, we can set the line width.
Properties to two, you can run the section, and see the thicker plot. Of course, we can experiment any core change. We can try to change the color to red and run again. When you click on the plot. You can quickly add a grid title or other annotations interactively. And also you can automatically upgrade the code with the corresponding comments.
Now, let's make a copy of this code because I'd like to show you an interesting pictures to use it. But first let me make it a bit more flexible with a couple of extrapyramidal. Maybe one for the Title, one for the color. Let's see if it works.
Now, select the code and then click on refactor. To convert the code automatically into a local function. You just needed to rename it. Note that any local function must be put at the end of the script and close by end. The lives creator will substitute a function called automatically.
And we can try to make a change and reduce the local function. So refactoring is very useful to modularize the code and make it more readable. To interact a bit more with our code we can include some UI control. For example, let's start with the parameter alpha that will control the level of noise we want to introduce. And redefine the signal by adding random numbers.
With a normal distribution, controlled by alpha. Reusing the local function created before it's certainly a good idea, to visualize and understand the effect of adding this noise to the signal. But how can we explore different values of alpha?
The easiest way to insert tab click controls, and choose one of the available controls. For example, a numeric slider we can set that the feasible range of values. The minimum the maximum and the incremental step.
And here's the magic any time we move the slider all the comments in that section where we execute. So it will regenerated a random noise and update the plot. So life controls can really help understand how a parameter impact on the model and gain a deeper insight.
Live Script can also speed up some common tasks in data analysis and preprocessing. Click tasks button choose one of the multiple tasks. For example, smooth data. And an interactive interface will show up. Where you can select the input data. Choose a smoothing method, or change it a smoothing factor and immediately see the output. You can experiment with another method adjusted the smoothing factor or try another method again until you get a good performance.
After this interaction you can see the automatically generated code. And clicking on the three dots, you have three options to see in the script to control only. The code only, or both controlling code together. As a conclusion, we are now ready to export their whole lives grouped into a shareable document. You can go to the top right bar and choose to hide a code if you want.
If you hide a code, the document will contain only text and output. But I'm not going to do this now. Just in case, we can choose to hide a code or not also from the View tab.
Now, go to Save button and choose Export to PDF or another format available-- Word, HTML, LDK. And after a few seconds, you can see the PDF completed with the Table of Contents, all the formatted text, and the output. And that's it. So for your reference, there is a nice cheat sheet downloadable from our MathWorks website. I hope this video was useful, and Happy Live Script.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.