Racing Lounge

Best practices and teamwork for student competitions

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the Original version of the page.

Live Editor and Live Scripts 4

Posted by Christoph Hahn,

At the beginning of preparation for this post, I asked Lars Schatkowski, the co-author today, to summarize the benefits of the MATLAB Live Editor in a single phrase. He seemed to be expecting this one and replied: “Christoph, it is actually a new way of coding, because it includes the output of an algorithm to the script itself!” So, no matter how you are using MATLAB today, we think familiarizing yourself with the MATLAB Live Editor is beneficial.

By the way, all the credit for preparing the content goes to Lars. Thanks very much!

MATLAB code and report in a single document

Script isn’t just ASCI text anymore but an interactive document that combines MATLAB code, inline output, formatted text, equations, and images in a single environment.

Writing MATLAB code and documenting the outcome at the same time is a great time-saver. I have experienced that project teams with high fluctuation (fancy description for automotive student teams, isn’t it?) have to be good at documenting their know-how or they will become less competitive over time. I imagine that quasi-automated report generation can be a strong asset for longterm success.

Among the many features, the Live Editor for example allows to:

  • Format code and comments like you would do with a word processor
  • Obtain compact output next to the code that produces it
  • Intuitively manipulate plots
  • Write equation using LaTeX or an equation editor

[Click and magnify the images to explore how live editor may look like.]

Embedding a GIF into live editor

Writing equations using LaTeX nomenclature

Interactive plotting

Jumpstart into analytical equation solving

Live editor is also a powerful user interface to work with equations. They look exactly the same as you would write them with pen on paper. That simplifies finding typos compared to equations represented by ASCI text a lot. Think about your calculus exam and imagine MATLAB differentiating and re-arranging equations for you.  Symbolic Math Toolbox™ is now tied a lot closer to your MATLAB user interface.

Convert all your code to live scripts?

Should you now convert all scripts to live scripts? You certainly could, but that’s probably not the most practical solution. At first, for code that requires a fair part of documentation, it is beneficial to use live scripts! Just think about ramping-up new team members and resulting time savings. Secondly, think about reports that you are going to create. That tasks drops from your plate completely with live scripts. On the other hand, scripts or functions that are well-defined for a certain purpose may remain as they are and can easily be called from live scripts.

Debugging

How about debugging? No worries! You still can work with >>dbstop, >>dbstep and Co. Currently, the visual toolbar alternatives such as setting breakpoints, step in or out are not support but you can ‘enable’ that by saving the live scripts as scripts.

A new and noteworthy alternative is what I call “visual debugging”. This is an extremely intuitive method of understanding how the code works. Removing certain semicolons reveals the output right next to your code and you can see how errors evolve compared to only seeing the error message after the script has failed.

Conclusion

Let me again try to summarize in a single phrase: Nothing is turned to the negative and you can benefit a lot.

Find here some links to related resources:

  • Examples in the documentation
  • Release notes with new features being added
    • R2016b: Pan, zoom, and rotate axes in output figures. Create and edit equations interactively using the equation editor. Create new sections and format text quickly using auto formatting. Automatically rename all functions or variables in a live script. Drag and drop selected code and text within a live script and between other applications. View outputs sooner when running live scripts
    • R2017a: Edit a figure interactively including title, labels, legend, and other annotations. Get suggestions for mistyped commands and variables. Copy live script outputs to other applications. Hover over variables to see their current value
  • Live Editor in the browser

Feel invited to start and join a discussion. I am curious to learn whether things are applicable to your work and what else you would need.

Cheers Christoph

4 CommentsOldest to Newest

yao gang replied on : 1 of 4
Hi,the link of [the Live Editor in the browser] in the [Conclusion] seems not correct: The page you were looking for does not exist. Use the search box or browse the examples below to find the page you were looking for. Do you mean this one? http://www.mathworks.com/help/symbolic/examples/integration.html Live Script works very well with  Symbolic Math Toolbox™,it will be better if it can run faster. :)
Christoph Hahn replied on : 2 of 4
Hi Yao Gang, thanks very much, good catch! I just updated the link, seems that they have changed the URLs. Cheers
Vol Kan replied on : 3 of 4
Hello, I am trying to figure out how to run a code, which is written in latex format in Matlab. I do know how to take the mathematical expression, which is written in latex, from latex and paste it to the live editor. But having difficulties to run that code in the live editor. Is it possible for you to share a simple example and run it with for loop starting from 1:40.
Christoph Hahn replied on : 4 of 4
Hi Vol Kan, interesting question! MATLAB won't allow you to solve equations that you inserted using LaTeX commands because these are part of the text domain and not part of the code domain in a live script. The feature is meant to create visually appealing formulae. However, the following might interest you. When it comes to solving equations in MATLAB there is an 'alternative way' called symbolic math. The standard approach is a solving equations numerically based on approximating solutions. Let me quote from the product information "Symbolic Math Toolbox™ enables you to perform symbolic computations from the MATLAB® command line by defining a special data type — symbolic objects. Functions are called using the familiar MATLAB syntax and are available for integration, differentiation, simplification, equation solving, and other mathematical tasks. Symbolic computations augment the numeric capabilities of MATLAB. You can convert symbolic results to numeric results and use them outside Symbolic Math Toolbox." https://www.mathworks.com/help/symbolic/symbolic-computations-in-matlab.html There are a few examples in the documentation, such as this one: https://www.mathworks.com/help/symbolic/examples/compute-binomial-coefficients-exactly.html. As I am somewhat rambling here with my reply, please let me know whether I anticipated correctly what you were asking. Best, Christoph