matlab-uihtml-app-builder
The MATLAB uihtml App Builder skill provides guidance for creating interactive web applications that combine HTML/JavaScript user interfaces with MATLAB computational backends using the uihtml component. Use this skill when building web-based MATLAB applications, responsive graphical user interfaces with HTML/CSS/JavaScript, data visualization tools, or form-based applications that require MATLAB processing power, particularly when users specifically request uihtml, HTML interfaces, JavaScript integration, or interactive GUI creation.
git clone --depth 1 https://github.com/matlab/agent-skills-playground /tmp/matlab-uihtml-app-builder && cp -r /tmp/matlab-uihtml-app-builder/skills/matlab-uihtml-app-builder ~/.claude/skills/matlab-uihtml-app-builderSKILL.md
# MATLAB uihtml App Builder
This skill covers how to build interactive web applications that combine HTML/JavaScript interfaces with MATLAB computational backends using the uihtml component. The HTML side handles the UI; MATLAB does the computation.
## When to Use This Skill
- Building interactive MATLAB apps with HTML/JavaScript interfaces
- Creating web-based UIs for MATLAB applications
- Building responsive MATLAB GUIs with HTML/CSS/JS
- When user mentions: uihtml, HTML, JavaScript, web app, web interface, interactive GUI
- Combining web UI design with MATLAB computational power
- Creating calculator apps, data visualizers, or form-based MATLAB tools
## Core Architecture
### The Four Components
1. **HTML Interface** - User interface with buttons, forms, displays
2. **JavaScript Logic** - Event handling and UI interactions
3. **MATLAB Backend** - Computational engine and data processing
4. **uihtml Component** - Bridge between HTML and MATLAB
### Communication Patterns
The uihtml component enables bidirectional communication between JavaScript and MATLAB through several mechanisms:
#### Pattern 1: MATLAB → JavaScript (Data Property)
**Use Case**: Sending data from MATLAB to update the HTML interface
```matlab
% MATLAB side
h.Data = "Hello World!";
```
```javascript
// JavaScript side
htmlComponent.addEventListener("DataChanged", function(event) {
document.getElementById("display").innerHTML = htmlComponent.Data;
});
```
#### Pattern 2: JavaScript → MATLAB (Events)
**Use Case**: Triggering MATLAB functions from user interactions
```javascript
// JavaScript side - send event to MATLAB
htmlComponent.sendEventToMATLAB("Calculate", expression);
```
```matlab
% MATLAB side - receive and handle event
h.HTMLEventReceivedFcn = @handleEvent;
function handleEvent(src, event)
eventName = event.HTMLEventName;
eventData = event.HTMLEventData;
% Process event...
end
```
#### Pattern 3: MATLAB → JavaScript (Custom Events)
**Use Case**: Sending computed results or status updates to JavaScript
```matlab
% MATLAB side - send custom event to JavaScript
sendEventToHTMLSource(h, "ResultChanged", result);
```
```javascript
// JavaScript side - listen for custom event
htmlComponent.addEventListener("ResultChanged", function(event) {
document.getElementById("display").textContent = event.Data;
});
```
#### Pattern 4: Complex Data Transfer
**Use Case**: Passing structured data between MATLAB and JavaScript
```matlab
% MATLAB side - struct data gets JSON encoded automatically
itemData = struct("ItemName","Apple","Price",2,"Quantity",10);
h.Data = itemData;
```
```javascript
// JavaScript side - access as object properties
htmlComponent.Data.ItemName // "Apple"
htmlComponent.Data.Price // 2
htmlComponent.Data.Quantity // 10
```
**Important: decoding is automatic in both directions.**
- A JS object sent via `sendEventToMATLAB` arrives on `event.HTMLEventData` already converted to a MATLAB struct. **Do not call `jsondecode`**; it will fail on a struct.
- A MATLAB struct sent via `sendEventToHTMLSource` arrives on `event.Data` already as a JavaScript object. **Do not call `JSON.parse`**; it will fail on an object.
- Field names round-trip exactly: a JS `{x0: 1}` becomes a MATLAB `struct('x0', 1)`, not `struct('x_0', ...)` or similar.
- Numeric scalars arrive as MATLAB `double`. Wrap field reads with `double(data.x0)` if you want to be defensive about types.
## Critical Rules
### Security Requirements
- **ALWAYS** set `HTMLSource = 'trusted'` when using local HTML files:
```matlab
h.HTMLSource = fullfile(pwd, 'myapp.html');
% This is treated as trusted automatically for local files
```
- **MUST** validate all input from JavaScript before processing in MATLAB
- **NEVER** use `eval()` on user input without strict sanitization
- **ALWAYS** restrict allowed characters in user input for expressions
### Error Handling
**ALWAYS wrap MATLAB event handlers in try-catch blocks:**
```matlab
function handleEvent(src, event)
eventName = event.HTMLEventName;
eventData = event.HTMLEventData;
try
% Process the event
result = processData(eventData);
% Send result back to JavaScript
sendEventToHTMLSource(src, 'ResultEvent', result);
catch ME
% Handle errors gracefully
fprintf('Error: %s\n', ME.message);
sendEventToHTMLSource(src, 'ErrorEvent', ME.message);
end
end
```
### Data Validation
**ALWAYS validate user input before processing:**
```matlab
function result = validateExpression(expression)
allowedChars = '0123456789+-*/.() ';
if ~all(ismember(expression, allowedChars))
error('Invalid characters in expression');
end
% Additional validation...
result = true;
end
```
### File Organization
**Follow this directory structure:**
```
project/
├── app.m # Main MATLAB function
├── app.html # HTML interface
├── README.md # Usage instructions
└── examples/ # Additional examples (optional)
```
## Complete Examples
### Example 1: Simple Calculator App
**MATLAB Side (calculator.m):**
```matlab
function calculator()
% Create main figure
fig = uifigure('Name', 'Calculator', 'Position', [100 100 400 500]);
% Create HTML component
h = uihtml(fig, 'Position', [25 25 350 450]);
h.HTMLSource = fullfile(pwd, 'calculator.html');
h.HTMLEventReceivedFcn = @(src, event) handleEvent(src, event);
end
function handleEvent(src, event)
eventName = event.HTMLEventName;
eventData = event.HTMLEventData;
try
switch eventName
case 'Calculate'
% Validate input
expression = char(eventData);
allowedChars = '0123456789+-*/.() ';
if ~all(ismember(expression, allowedChars))
error('Invalid characters in expression');
end
% Evaluate safely
result = eval(expression);Use this skill when the user wants to author, design, scope, or refine an Agent Skill (a SKILL.md file). Trigger phrases include "build a new skill", "design an agent skill", "scope a SKILL.md", "how should I structure this skill", "write a skill for X", "my skill isn't working well", or any request to improve an existing SKILL.md. Walks the user through an empirical, test-first process — probe the agent for real failures, design only for genuine knowledge gaps, iterate against runnable examples, and verify across models.
>
Use when a learner asks for help with MATLAB homework, labs, projects, graded assignments, take-home exams, quizzes, or any programming task where academic integrity, course policy, or instructor constraints may limit direct solutions. Use to provide policy-aware hints, conceptual coaching, partial feedback, and assignment-safe MATLAB tutoring.
Use when tutoring a learner through MATLAB debugging, error interpretation, failed tests, incorrect outputs, array-shape problems, indexing mistakes, function argument issues, or code repair practice. Use for guided debugging sessions, debugging drills, teach-the-agent critique, and evidence-based MATLAB troubleshooting.
Use when an AI tutor session concerns MATLAB programming concepts, MATLAB syntax, MATLAB errors, MATLAB code style, MATLAB projects, or MATLAB toolbox workflows.
Use when an instructor wants to create, interview for, configure, install, update, or review a course AI-use policy for MATLAB AI tutoring. Produces an AI-POLICY.md file for LMS sharing and local tutoring-session enforcement by assignment guardrails.
Create MATLAB Course Designer MATLAB Exercise learning activities by wrapping the existing matlab-generate-grader-assessments skill, then validating generated solution.m, template.m, and tests.m files with MATLAB MCP Server tool calls. Use when the user asks to create a MATLAB Exercise, MATLAB course activity, Course Designer MATLAB activity, validated MATLAB solution file, or MATLAB Exercise component folder.
Use when prompting a learner to complete hands-on MATLAB coding exercises, guided practice, debugging drills, code tracing, small MATLAB projects, or MATLAB-script assessment during tutoring. Use when the tutor should create a complete runnable MATLAB script, execute it through MATLAB tools, compare the produced outputs with expected outputs, and evaluate MATLAB programming style.