Skills matlab-live-script
Create MATLAB plain text Live Scripts (.m files) following specific formatting rules. Use when generating MATLAB scripts, educational MATLAB content, Live Scripts, or when the user requests .m files with rich text formatting.
git clone https://github.com/matlab/skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/matlab/skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/matlab-live-script" ~/.claude/skills/matlab-skills-matlab-live-script && rm -rf "$T"
skills/matlab-live-script/SKILL.mdMATLAB Plain Text Live Script Generator
This skill provides comprehensive guidelines for creating properly formatted MATLAB plain text Live Scripts. These scripts combine executable MATLAB code with rich text documentation in a single .m file.
When to Use This Skill
- Creating new MATLAB Live Scripts in plain text format
- Generating educational MATLAB content with explanations
- When the user requests .m files with documentation
- Converting code examples into Live Script format
- Creating tutorial or example scripts
Critical Rules
File Format
- ALWAYS use the .m suffix for plain text Live Scripts
- NEVER create .mlx scripts (binary format)
- MUST close every script with the required appendix
Required Appendix
Every Live Script must end with this exact formatting:
%[appendix]{"version":"1.0"} %--- %[metadata:view] % data: {"layout":"inline"} %---
Reading Live Scripts (Token Optimization)
When reading a Live Script file to pass back to the language model, you can save significant token count by ignoring everything below the appendix marker (which begins with
%[appendix]). This optimization avoids passing large embedded images that are stored in the appendix section. All working code and text content appears before the appendix, so no functional information is lost.
Formatting Rules
Section Headers
CORRECT format:
%% %[text] ## Section Title
INCORRECT format (DO NOT USE):
%% Section Title
Rich Text
- Normal text uses
prefix%[text] - Text intended for a single paragraph should appear on a single line
- Use Markdown formatting after
%[text] - DO NOT leave blank lines in the file
Bulleted Lists
Bulleted lists must have a backslash on the last item:
%[text] - bullet 1 %[text] - bullet 2 %[text] - bullet 3 \
Tables
%[text:table] %[text] | Column A | Column B | %[text] | --- | --- | %[text] | Value 1 | Value 2 | %[text] | Value 3 | Value 4 | %[text:table]
LaTeX Equations
Format equations with double backslashes:
%[text] $ e = \\sum_{\\alpha=0}^\\infty \\alpha^n/n! $
Note: All backslashes in LaTeX must be doubled.
Comments for Readers
DO NOT use fprintf for reader comments:
fprintf('This is a comment') % WRONG
Instead use rich text:
%[text] This is a comment % CORRECT
Code Guidelines
Figures and Plots
- Use implicit figure creation (just call plot, histogram, etc.)
- DO NOT use the
command to create new figuresfigure - Put no more than one plot per section (unless using tiled layouts)
- Only use tiled plots when especially important to the illustration
Script Initialization
- DO NOT start scripts with
orclose all
commandsclear - Let MATLAB handle workspace management
Complete Example
%[text] # Sinusoidal Signals %[text] Examples of sinusoidal signal in MATLAB. %[text] - sine waves %[text] - cosine waves \ x = linspace(0,8*pi); %% %[text] ## Sine Wave plot(x,sin(x)) title('Sine Wave') xlabel('x (radians)') ylabel('sin(x)') grid on %% %[text] ## Cosine Wave plot(x,cos(x)) title('Cosine Wave') xlabel('x (radians)') ylabel('cos(x)') grid on %[text] %[appendix]{"version":"1.0"} %--- %[metadata:view] % data: {"layout":"inline"} %---
Structure Pattern
A typical Live Script follows this pattern:
-
Title and Introduction
%[text] # Main Title %[text] Brief description of what this script does. -
Setup Code (if needed)
variable = value; data = load('file.mat'); -
Sections with Explanations
%% %[text] ## Section Name %[text] Explanation of what this section does. code_goes_here(); plot(results) -
Required Appendix
%[appendix]{"version":"1.0"} %--- %[metadata:view] % data: {"layout":"inline"} %---
Common Patterns
Mathematical Explanations with Equations
%[text] ## Theory %[text] The discrete Fourier transform is defined as: %[text] $ X(k) = \\sum_{n=0}^{N-1} x(n)e^{-j2\\pi kn/N} $
Code with Inline Comments
%% %[text] ## Data Processing %[text] First, we load and filter the data. data = load('measurements.mat'); filtered = lowpass(data, 0.5); % Apply lowpass filter %[text] Then we visualize the results. plot(filtered) title('Filtered Data')
Multiple Related Plots (Tiled Layout)
Only when necessary for comparison:
%% %[text] ## Comparison of Methods tiledlayout(1,2) nexttile plot(method1) title('Method 1') nexttile plot(method2) title('Method 2')
Checklist
Before finishing a Live Script, verify:
- File has .m extension
- Sections use
followed by%%%[text] ## - No blank lines in the file
- Bulleted lists end with backslash
- LaTeX uses double backslashes
- No
commandsfigure - No
orclose all
at startclear - Appendix is present and correctly formatted
- No
for comments (usefprintf
instead)%[text]
Troubleshooting
Issue: Script doesn't display rich text properly
- Solution: Ensure
is at the start of each text line%[text]
Issue: Equations not rendering
- Solution: Check that all backslashes are doubled in LaTeX
Issue: Sections not appearing correctly
- Solution: Use
on its own line, then%%
on the next line%[text] ##
Issue: Script won't save with outputs
- Solution: Verify appendix is exactly as specified, with proper indentation