matlab/matlab-agentic-toolkit/matlab-diagnose-parfor
Claude Code and agent skill by matlab/matlab-agentic-toolkit
- community
Checking that the SKILL.md file is still in the repository.
Diagnose and fix parfor errors in MATLAB. Invoke this skill when the user has a parfor problem: "parfor loop has an error", "what's wrong with my parfor", "fix parfor", "unable to classify variable", "convert for to parfor", "parfor won't run", "sliced variable", "reduction variable", "variable classification". Also invoke when you read a .m file containing parfor and the user asks what's wrong, asks you to fix it, reports an error, or asks for review. Do NOT invoke for parfor performance quest…
Install
npx vskill@latest install matlab/matlab-agentic-toolkit/matlab-diagnose-parforSKILL.md
Diagnose and Fix parfor Variable Classification
Do NOT reason about parfor classification rules from memory. LLM training data contains incomplete and often incorrect explanations of these rules. MATLAB's code analyser (checkcode / codeIssues / mcp__matlab__check_matlab_code) is the only reliable authority — it implements the exact same classifier that the runtime uses. Always run it first; never guess.
When to Use
- ANY code involving
parforwhere the user reports a problem or asks for help - User gets a parfor classification error (runtime or in editor)
- User wants to convert a
forloop toparfor - User asks why parfor complains, won't run, or is slow due to classification
- You are about to suggest changes to parfor code — run the analyser first
When NOT to Use
- parfor runs but is slow — performance/pool-choice question, not a classification error
- Worker needs setup/state (paths, environment, toolboxes) — not a parfor variable problem
- Code simply mentions
parforbut the user does not mention any problems with it
Workflow
Always follow this order. Never guess at classification rules.
- Run the code analyser on the file containing the parfor loop using
the mcp__matlab__check_matlab_code MCP tool. Do NOT shell out to MATLAB via Bash — always use the MCP tool. Read the exact error message.
- Identify the error type from the message (see table below).
- Apply the matching fix pattern (see Patterns section).
- Verify by re-running
mcp__matlab__check_matlab_codeon the fixed
code — confirm zero parfor classification errors.
Error Messages and Root Causes
For the full catalog of all 48 parfor analyser messages (with IDs), see references/parfor-analyser-messages.md. The table below covers the most common classification errors.
| checkcode message | Root cause | Fix pattern | |
|---|---|---|---|
| "Unable to classify variable 'X'" | Catch-all: many causes. Often invalid slicing (inconsistent subscripts, indexing not involving the loop variable) or invalid combinations of accesses (e.g. indexed reduction). Struct-field accesses are one specific way to trigger this. | Identify the specific access pattern, then restructure to fit one category or split conflicting roles | |
| "multiple sliced accesses...do not all have the same list of subscripts" | Same variable sliced with different subscript patterns (e.g. A{i,1}, A{i,2}) | Use single assignment: A(i,:) = {...} | |
| "Variable 'X' is indexed...but it is not a valid sliced output variable" | One subscript is non-deterministic (random, function call), or loop variable appears in multiple dimensions, or subscript pattern otherwise violates slicing rules | Pre-compute problematic subscripts, restructure to use loop variable in exactly one dimension with fixed remaining subscripts | |
| "accessed with an invalid combination of sliced indexing expressions and non-indexed reads" | Variable is written with sliced indexing AND read whole in the same iteration — conflicting roles | Separate into two variables: one for sliced writes, a snapshot or broadcast copy for whole reads | |
| "The temporary variable 'X' might be used after the PARFOR loop" / "Temporary variable 'X' must be set inside the parfor loop before it is used" | Variable is conditionally assigned (only in an if-branch), so MATLAB treats it as temporary — but it's used after the loop or read before being set on some paths | Restructure as sliced output (store per-iteration values) + post-loop reduction | |
| "When indexing a sliced variable with a nested for loop variable, the range...must be a row vector of positive constant numbers or variables" | Inner for j = 1:(expr) where expr is not a literal constant or a broadcast variable | Make the inner loop range a constant or broadcast variable, or accumulate into a local temporary and assign the sliced variable once per outer iteration | |
| "The entire array or structure 'X' is a broadcast variable" | Variable used whole (not indexed by loop variable); read-only | Not an error — performance warning. Use parallel.pool.Constant(X) if large |
Variable Classification Rules (Reference)
parfor classifies each variable into exactly one category:
| Category | Rule | Example | ||
|---|---|---|---|---|
| Loop | The loop index variable | parfor i = 1:N | ||
| Sliced | Indexed by the loop variable in any one dimension (not just the first); the same subscript pattern is used in every access. The loop variable may be offset by a literal constant or a broadcast variable (e.g. A(i+1,:), A(:,i-k) where k is broadcast). | A(i,:) = ..., A(:,i) = ..., A(i+1) = ... | ||
| Broadcast | Used whole (not indexed by loop variable); read-only | total = sum(bigArray) | ||
| Reduction | Form X = X op expr or X = expr op X where X is the entire variable, op is associative operator +, .*, &, `\ | ` etc. | total = total + val | |
| Temporary | First use in iteration is an assignment; not used after loop | temp = rand(10) |
Critical constraint: A variable must fit exactly ONE category. Mixed usage (e.g. struct fields used as reductions, or same variable sliced with different subscripts) causes classification failure.
Patterns
Fix 1: Struct field as sliced output → plain array
Error: "Unable to classify variable 'out'"
The most common cause of "Unable to classify" with structs. Users write out.field(i) = someFcn() expecting to slice into a struct field, but first-level indexing on out is dot (.field), not parentheses. Sliced variables require first-level () or {} indexing with the loop variable — dot-indexing is never valid for slicing.
Fix: Use separate arrays for sliced output, pack into struct after the loop:
A related variant: struct fields used as reductions (results.sum = results.sum + val). The same rule applies — extract each field into its own scalar variable, reduce independently, then pack the struct after the loop.
Fix 2: Multiple sliced subscripts → single assignment
Error: "multiple sliced accesses...do not all have the same list of subscripts"
Fix: Single parenthesis-indexed assignment with consistent subscripts:
Note: use () parenthesis indexing (assigns cells), not {} brace indexing when assigning multiple cells.
Fix 3: Invalid sliced subscript patterns
Error: "Variable 'X' is indexed...but it is not a valid sliced output variable"
A sliced variable must be indexed by the loop variable in exactly one dimension, with the remaining subscripts fixed (literals, broadcast variables, or colon). Three common violations:
Fixes:
(a) Pre-compute non-deterministic indices before the parfor, or accumulate into a local temporary and assign a full row once:
(b) Remove the duplicate loop-variable dimension — restructure so i indexes only one dimension:
(c) Make the nested-for range a constant or broadcast variable, OR collect into a local variable and assign the sliced output once:
Fix 4: Sliced write combined with full read
Error: "Unable to classify variable 'data'"
A sliced read combined with a broadcast (whole) read is legal — the variable simply "decays" to broadcast in that case. The unclassifiable combination is a sliced write together with a full read of the same variable in the same iteration. Often the whole read is simply attempting to extract size or type information from the sliced output variable.
Fix: Extract the constant information needed ahead of the loop.
Note that if the "whole read" of a sliced output variable depends on the actual values within the variable, then this fix is not appropriate, and may imply that iterations are not independent.
Fix 5: Reduction not in standard form
Error: "Unable to classify variable 'X'"
A reduction must be exactly: X = X op expr or X = expr op X or X = fcn(X, expr) where X is the entire variable (no indexing) and op is an associative operator +, .* etc.; fcn can be any associative function such as union, max etc.
Fix: Accumulate per-iteration, then reduce:
Fix 6: Temporary variable incorrectly flagged
If a variable is assigned before use in every iteration but checkcode still warns, ensure the first reference in the iteration is a full assignment (not indexed):
Fix: Create temp in single assignment
Fix 7: Conditional update that needs to survive the loop
Error: "The temporary variable 'X' might be used after the PARFOR loop"
Any temporary variable is unavailable after the loop — this is a fundamental parfor rule, not specific to conditional assignment. The "best so far" pattern fails because bestscore = score inside a conditional doesn't match reduction form (X = X op expr), so the classifier cannot treat it as a reduction. It falls through to temporary, and temporaries are discarded when the loop ends:
Fix: Compute all candidates as sliced output, then reduce after the loop:
Conventions
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills-catalog/parallel-computing/matlab-diagnose-parfor/SKILL.md
Popularity
- Repository stars
- 1,028
- Repository forks
- 128
- 7-day trend score
- 35
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/matlab/matlab-agentic-toolkit/matlab-diagnose-parfor)