{"id":10139,"date":"2025-09-23T11:52:13","date_gmt":"2025-09-23T15:52:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=10139"},"modified":"2025-09-23T12:05:02","modified_gmt":"2025-09-23T16:05:02","slug":"making-live-scripts-with-generative-ai","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2025\/09\/23\/making-live-scripts-with-generative-ai\/","title":{"rendered":"Making Live Scripts with Generative AI"},"content":{"rendered":"<p>I've been having fun making MATLAB code with Large Language Models. You can try this yourself with <a href=\"https:\/\/www.mathworks.com\/products\/matlab-copilot.html\">MATLAB's Copilot feature<\/a>. But since the AI world is moving so fast, I also like to do a little generative AI tourism, trying out the latest models and tools. Today I want to look at Anthropic's Claude Code.<\/p>\n<p>Claude Code runs in a terminal window that can sit inside an IDE like Microsoft's VS Code. You do need a payment plan with Anthropic in order to make it work. So be aware that the demos I'm stepping through here involved me spending a little money. The total cost of the experiments I describe here was $0.63 using Claude's pay-as-you-go option.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"400\" height=\"281\" class=\"alignnone size-full wp-image-10166\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/dejong1.png\" alt=\"\" \/><\/p>\n<p>What to code? Since we're just playing around, let's take a visually appealing topic: fractal attractor systems. I started off with a vague prompt, curious to see what it would come up with.<\/p>\n<p style=\"padding-left: 40px;\"><em>&gt; Make me a MATLAB Live Script demonstrating the De Jong fractal attractor<\/em><\/p>\n<p>The De Jong attractor is a two-dimensional system defined by these equations.<\/p>\n<p>x<sub>t+1<\/sub> = sin(a \u00b7 y<sub>t<\/sub>) - cos(b \u00b7 x<sub>t<\/sub>)<br \/>\ny<sub>t+1<\/sub> = sin(c \u00b7 x<sub>t<\/sub>) - cos(d \u00b7 y<sub>t<\/sub>)<\/p>\n<p>Claude gets right back to me with a result, DeJongFractalAttractor.mlx, but something is wrong.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-10169\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/error.png\" alt=\"\" width=\"294\" height=\"127\" \/><\/p>\n<p>Claude (I'm using the Sonnet 4 model here) knows about MATLAB's Live Scripts. And it knows that Live Scripts use the .MLX suffix. But it doesn't know how to create the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/live-script-file-format.html\">required OPC format<\/a>. Instead it writes out a plain text MATLAB script and gives it the .MLX suffix. The resulting file won't even open in MATLAB. If I change the suffix to .M, the file is revealed to be an old-school plain text MATLAB script, complete with %% section breaks.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"954\" height=\"253\" class=\"alignnone size-full wp-image-10172\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/code1.png\" alt=\"\" \/><\/p>\n<p>I can use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/publish.html\">PUBLISH<\/a> command to turn this file into HTML, but we can do better now! One of my favorite recent additions to MATLAB is the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/plain-text-file-format-for-live-scripts.html\">plain text Live Script<\/a>. With this format I get the best of all worlds. All the code, rich text, and images are in a single notebook file, so I don't have a dead HTML file with numerous nearby images hiding in a subfolder. Furthermore, that single file is a plain text .M file! So I don't have the hassle of a complex .MLX format that GitHub can't understand.<\/p>\n<p>So after I change the suffix to .M and then open it as a Live Script and then save it as a MATLAB Live Code File .M file, I end up with something that looks pretty good. But that's a lot of steps to go through just to get to a decent output!<\/p>\n<p>So this generative AI thing is kind of a bust, eh? After all, it fumbled my request. But wait! Here is the main point I want to make today: with some extra prompting, you can often teach the AI how you want it to behave. Out of the box, Claude doesn't know about plain text Live Script files. But I can teach it. I put some coaching prompts in a markdown file called Live_Script_rules.md. Here are some of the contents of that file.<\/p>\n<pre>## Live Scripts\r\n- If creating a script, use the plain text Live Script format with the .m suffix\r\n- DO NOT create .mlx scripts\r\n- DO NOT create .m scripts that are not Live Scripts\r\n- When creating new plain text Live Scripts, close with an appendix:\r\n    %[appendix]{\"version\":\"1.0\"}\r\n    %---\r\n    %[metadata:view]\r\n    %\u00a0 \u00a0data: {\"layout\":\"inline\"}\r\n    %---\r\n- Section heads should look like this:\r\n    %%\r\n    %[text] ## Section Title\r\n- DO NOT do this: `%% Section Title`\r\n- Normal text intended for a single paragraph should appear on a single line\r\n- Equations in Live Scripts are formatted using LaTeX formtting like this.\r\n    %[text] $ e = \\\\sum\\_{\\\\alpha=0}^\\\\infty \\\\alpha^n\/n! $\r\n- Note the double backslashes\r\n- Use implicit figure creation\r\n- DO NOT use the \"figure\" command to create new figures\r\n- DO NOT start scripts with CLOSE and CLEAR commands\r\n- Bulleted lists in a rich text block must close with a backslash. Here is an example.\r\n    %[text] - bullet 1\r\n    %[text] - bullet 2\r\n    %[text] - bullet 3 (note the trailing backslash for the last bullet) \\<\/pre>\n<p>I can go back to Claude and say<\/p>\n<p style=\"padding-left: 40px;\"><em>&gt; Please regenerate that file, but using these rules: @Live_Script_rules.md<\/em><\/p>\n<p>Now the situation is much improved. As long as I keep this rules file in context, I can create beautiful new plain text Live Script files all day long. Here's one last prompt to show what I mean.<\/p>\n<p style=\"padding-left: 40px;\"><em>&gt; Use the file DeJongFractalAttractor.m as a template and generate two more similar files for these attractors: Dadras attractor and Aizawa attractor<\/em><\/p>\n<p>Since my rules were still in context, I was immediately rewarded with two more Live Scripts that met all my standards, no post-editing required.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"301\" class=\"alignnone size-full wp-image-10175\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/three_demos.png\" alt=\"\" \/><\/p>\n<p>I have found it very useful to remind myself that, whenever I'm not happy with the AI model's generated output, there is often a prompt that can rescue the situation. Once you find that extra prompt, you can make it a part of your practice, and the world gets better very quickly. So consider hesitating a bit before you declare a model to be stupid. Before you've made an effort to help it out. You might be able to salvage the situation and emerge victorious.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/dejong1.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>I've been having fun making MATLAB code with Large Language Models. You can try this yourself with MATLAB's Copilot feature. But since the AI world is moving so fast, I also like to do a little... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2025\/09\/23\/making-live-scripts-with-generative-ai\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/10139"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=10139"}],"version-history":[{"count":14,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/10139\/revisions"}],"predecessor-version":[{"id":10193,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/10139\/revisions\/10193"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=10139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=10139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=10139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}