matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-classify-tabular-data
matlab-classify-tabular-data
UnverifiedUse this skill to classify tabular data end-to-end in MATLAB — load a dataset, prepare and clean it, select promising classifiers, train them, and compare accuracies with cross-validation, holdout, or hyperparameter optimization plus statistical tests. TRIGGER when: user asks to classify tabular data, pick classifiers for a dataset, compare classifier accuracy, run cross-validation or a holdout evaluation, or find the best model with statistical uncertainty. DO NOT TRIGGER when: user has non-tabular inputs (images, sequences, time series), wants a regression model, is training a specific neural network architecture (use matlab-train-network), or wants cost-sensitive learning or an arbitrary class-prior vector (this skill only supports the built-in uniform-prior toggle for imbalanced data).
scripts/helpers/export_workflow_script.m
View on GitHubfunction exportedPath = export_workflow_script(scriptPath, config)
% EXPORT_WORKFLOW_SCRIPT Emit a self-contained retraining workflow.
%
% EXPORTED = EXPORT_WORKFLOW_SCRIPT(SCRIPTPATH, CONFIG) writes a MATLAB
% script at SCRIPTPATH that reproduces the trained-model workflow on
% new data drawn from the same distribution. The generated script
% loads a user-supplied `.mat` file containing `Xnew`/`Ynew` (or a
% table with a designated response variable), applies the same
% preproc ops, rebuilds the modelDefs, trains only the selected
% models, applies single-shot HPO where requested, applies boosting
% resume where requested, and saves the results.
%
% CONFIG is a scalar struct with fields:
% .skillPath char, absolute path to this skill directory
% .selectedNames cellstr, subset of modelDefs names to train
% .preproc struct array recorded during Step 2
% .interpretability char, 'low' or 'high'
% .useUniformPrior logical
% .hpoModels (optional) cellstr — models to re-run HPO
% for on the full training set. Default {}.
% .resumeCycles (optional) struct — fieldname = sanitized
% modelDef name, value = extra cycles from
% Step 10 resumes. Default struct().
% .useImbalancedOverlay (optional) logical — force-on override for
% the imbalanced boosting overlay. Default
% false. The emitted script rebuilds the
% overlay whenever the flags it recomputes on
% the new data are imbalanced
% (flags.isImbalanced) — the runtime analogue
% of the selection-time gate — so on
% same-distribution new data the overlay-only
% model names (RoughRUSBoost, RoughAdaBoost,
% etc.) resolve in modelDefs WITHOUT this flag.
% Set it true only to force the overlay on
% regardless of the recomputed flags (e.g. the
% interactive run applied it and you want it
% pinned even if the new data reads as
% balanced). Leaving it false is safe for the
% ordinary same-distribution case.
% .useCompact logical, true to compact saved models
% .modelSavePath char, where the emitted script will save
% its retrained models
% .datasetLabel char, short human-readable label for
% comments (e.g., 'fisheriris', 'tamayo')
%
% The generated script depends only on this skill directory being on
% the MATLAB path — it uses the same helpers (apply_preproc,
% build_model_definitions, retrain_with_hpo, retrain_with_resume,
% save_selected_models) the interactive workflow uses.
arguments
scriptPath (1,:) char
config (1,1) struct
end
lines = build_script_lines(config);
scriptDir = fileparts(scriptPath);
if ~isempty(scriptDir) && ~isfolder(scriptDir)
mkdir(scriptDir);
end
fid = fopen(scriptPath, 'w');
if fid < 0
error('export_workflow_script:cannotOpen', ...
'Failed to open %s for writing.', scriptPath);
end
cleaner = onCleanup(@() fclose(fid));
for k = 1:numel(lines)
fprintf(fid, '%s\n', lines{k});
end
exportedPath = scriptPath;
end
% -------------------------------------------------------------------------
function lines = build_script_lines(cfg)
hpoModels = get_default(cfg, 'hpoModels', {});
resumeCycles = get_default(cfg, 'resumeCycles', struct());
useOverlay = logical(get_default(cfg, 'useImbalancedOverlay', false));
lines = {};
lines{end+1} = '% Retraining workflow generated by matlab-classify-tabular-data.';
lines{end+1} = sprintf('%% Dataset context: %s', cfg.datasetLabel);
lines{end+1} = sprintf('%% Generated on: %s', string(datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss')));
lines{end+1} = '%';
lines{end+1} = '% Usage:';
lines{end+1} = '% 1. Save your new data as Xnew and Ynew in a .mat file, matching the';
lines{end+1} = '% shape of the training data used originally (same columns / features,';
lines{end+1} = '% same class labels). If X was a table, keep it a table with the same';
lines{end+1} = '% variable names.';
lines{end+1} = '% 2. Set NEW_DATA_PATH below to that .mat file.';
lines{end+1} = '% 3. Run this script. Retrained models will be saved to MODEL_SAVE_PATH.';
lines{end+1} = '';
lines{end+1} = '% ------------------ USER INPUT ------------------';
lines{end+1} = 'NEW_DATA_PATH = ''''; % <-- fill in path to .mat with Xnew, Ynew';
lines{end+1} = sprintf('MODEL_SAVE_PATH = %s;', mat_literal_char(cfg.modelSavePath));
lines{end+1} = sprintf('SKILL_PATH = %s;', mat_literal_char(cfg.skillPath));
lines{end+1} = '';
lines{end+1} = '% ------------------ LOAD DATA ------------------';
lines{end+1} = '% Match the interactive workflow''s no-addpath convention: cd into';
lines{end+1} = '% scripts/helpers/ so helpers resolve on the current working folder.';
lines{end+1} = 'ORIGINAL_DIR = pwd;';
lines{end+1} = 'cleanupCwd = onCleanup(@() cd(ORIGINAL_DIR));';
lines{end+1} = 'cd(fullfile(SKILL_PATH, ''scripts'', ''helpers''));';
lines{end+1} = 'D = load(NEW_DATA_PATH);';
lines{end+1} = 'assert(isfield(D, ''Xnew'') && isfield(D, ''Ynew''), ...';
lines{end+1} = ' ''Expected .mat file to contain Xnew and Ynew.'');';
lines{end+1} = 'X = D.Xnew;';
lines{end+1} = 'Y = D.Ynew;';
lines{end+1} = '';
lines{end+1} = '% ------------------ REPLAY PREPROC ------------------';
lines = [lines, emit_preproc_block(cfg.preproc)];
lines{end+1} = '[X, Y] = apply_preproc(preproc, X, Y);';
lines{end+1} = '';
lines{end+1} = '% ------------------ COMPUTE FLAGS ------------------';
lines{end+1} = '% Recompute flags on the new data. The original interpretability rating';
lines{end+1} = '% is preserved so the same model set is emitted.';
lines{end+1} = 'flags = compute_data_flags(X, Y);';
lines{end+1} = sprintf('flags.interp = %s;', mat_literal_char(cfg.interpretability));
lines{end+1} = 'if istable(X)';
lines{end+1} = ' flags.totalCatLevels = count_categorical_levels(X);';
lines{end+1} = 'else';
lines{end+1} = ' flags.totalCatLevels = 0;';
lines{end+1} = 'end';
lines{end+1} = '';
lines{end+1} = '% ------------------ BUILD MODEL DEFINITIONS ------------------';
lines{end+1} = sprintf('useUniformPrior = %s;', tf_literal(cfg.useUniformPrior));
% The emitted script decides at RUNTIME whether to rebuild the imbalanced
% boosting overlay, from the flags it recomputes on the new data — the
% runtime analogue of the selection-time gate in
% select-classifiers-imbalanced.md. This is what makes overlay-only
% recipes (RoughRUSBoost, RoughAdaBoost, ...) resolvable WITHOUT the
% caller having to hand-set a flag: on same-distribution new data
% flags.isImbalanced is true again, so the overlay is rebuilt exactly as
% it was at selection time. `useImbalancedOverlay` (from config) is a
% force-on override that stays true regardless — the interactive run sets
% it when it knows the overlay was used; left false it defers entirely to
% the recomputed flags.
lines{end+1} = '% Rebuild the imbalanced boosting overlay when the recomputed new-data';
lines{end+1} = '% flags say the data are imbalanced (the runtime analogue of the';
lines{end+1} = '% selection-time gate), or when the interactive run forced it on. This';
lines{end+1} = '% makes overlay-only recipes (RoughRUSBoost, RoughAdaBoost, ...) exist';
lines{end+1} = '% in modelDefs. The RUSBoost smallestClassSize gate is re-evaluated';
lines{end+1} = '% against the new data by the overlay itself.';
lines{end+1} = sprintf('useImbalancedOverlay = %s || flags.isImbalanced;', tf_literal(useOverlay));
lines{end+1} = 'if useImbalancedOverlay';
lines{end+1} = ' run(fullfile(SKILL_PATH, ''scripts'', ''model_catalog'', ''imbalanced_boosting.m''));';
lines{end+1} = ' modelDefs = build_model_definitions(flags, SKILL_PATH, ...';
lines{end+1} = ' ''X'', X, ''Y'', Y, ''UseUniformPrior'', useUniformPrior, ...';
lines{end+1} = ' ''ImbalancedOverlay'', IMBALANCED_BOOSTING_MODELS);';
lines{end+1} = 'else';
lines{end+1} = ' modelDefs = build_model_definitions(flags, SKILL_PATH, ...';
lines{end+1} = ' ''X'', X, ''Y'', Y, ''UseUniformPrior'', useUniformPrior);';
lines{end+1} = 'end';
lines{end+1} = '';
lines{end+1} = '% ------------------ TRAIN SELECTED MODELS ------------------';
lines{end+1} = sprintf('selectedNames = %s;', cellstr_literal(cfg.selectedNames));
lines{end+1} = sprintf('hpoModels = %s;', cellstr_literal(hpoModels));
lines = [lines, emit_resume_struct(resumeCycles)];
lines{end+1} = 'allNames = {modelDefs.name};';
lines{end+1} = 'trainedModels = cell(1, numel(modelDefs));';
lines{end+1} = 'for k = 1:numel(selectedNames)';
lines{end+1} = ' name = selectedNames{k};';
lines{end+1} = ' idx = find(strcmp(allNames, name), 1);';
lines{end+1} = ' assert(~isempty(idx), sprintf(''Model %s not in modelDefs on new data.'', name));';
lines{end+1} = ' if ismember(name, hpoModels)';
lines{end+1} = ' fprintf(''Retraining %s with single-shot HPO...\n'', name);';
lines{end+1} = ' trainedModels{idx} = retrain_with_hpo(modelDefs(idx), X, Y, useUniformPrior);';
lines{end+1} = ' else';
lines{end+1} = ' sanitizedName = sanitize_model_name(name);';
lines{end+1} = ' if isfield(resumeCycles, sanitizedName) && resumeCycles.(sanitizedName) > 0';
lines{end+1} = ' fprintf(''Retraining %s with %d extra cycles...\n'', name, resumeCycles.(sanitizedName));';
lines{end+1} = ' trainedModels{idx} = retrain_with_resume(modelDefs(idx), X, Y, ...';
lines{end+1} = ' resumeCycles.(sanitizedName), useUniformPrior);';
lines{end+1} = ' else';
lines{end+1} = ' fprintf(''Training %s...\n'', name);';
lines{end+1} = ' trainedModels{idx} = modelDefs(idx).fitFcn(X, Y);';
lines{end+1} = ' end';
lines{end+1} = ' end';
lines{end+1} = 'end';
lines{end+1} = '';
lines{end+1} = '% ------------------ SAVE ------------------';
lines{end+1} = sprintf('useCompact = %s;', tf_literal(cfg.useCompact));
lines{end+1} = 'metadata = struct( ...';
lines{end+1} = ' ''regenerated'', true, ...';
lines{end+1} = ' ''sourceDataset'', NEW_DATA_PATH, ...';
lines{end+1} = ' ''flags'', flags);';
lines{end+1} = 'savedPath = save_selected_models(selectedNames, modelDefs, trainedModels, ...';
lines{end+1} = ' X, Y, MODEL_SAVE_PATH, metadata, useCompact);';
lines{end+1} = 'fprintf(''Saved %d model(s) to %s\n'', numel(selectedNames), savedPath);';
lines{end+1} = '';
lines{end+1} = '% Note: HPO on the CV path uses single-shot HPO on the full training set';
lines{end+1} = '% (not the nested design from Step 11 of the interactive skill). Expect';
lines{end+1} = '% the accuracy from that interactive run to be a slight overestimate for';
lines{end+1} = '% this configuration.';
end
% -------------------------------------------------------------------------
function v = get_default(cfg, name, dflt)
if isfield(cfg, name)
v = cfg.(name);
else
v = dflt;
end
end
% -------------------------------------------------------------------------
function s = mat_literal_char(v)
s = ['''', strrep(v, '''', ''''''), ''''];
end
% -------------------------------------------------------------------------
function s = tf_literal(v)
if v
s = 'true';
else
s = 'false';
end
end
% -------------------------------------------------------------------------
function s = cellstr_literal(c)
if isempty(c)
s = '{}';
return
end
parts = cell(1, numel(c));
for k = 1:numel(c)
parts{k} = mat_literal_char(c{k});
end
s = ['{', strjoin(parts, ', '), '}'];
end
% -------------------------------------------------------------------------
function lines = emit_preproc_block(preproc)
lines = {};
lines{end+1} = 'preproc = struct(''op'', {}, ''payload'', {}, ''note'', {});';
for k = 1:numel(preproc)
op = preproc(k).op;
payload = preproc(k).payload;
note = '';
if isfield(preproc(k), 'note') && ~isempty(preproc(k).note)
note = preproc(k).note;
end
payloadLit = payload_literal(payload);
lines{end+1} = sprintf('preproc(end+1) = struct(''op'', %s, ''payload'', %s, ''note'', %s);', ...
mat_literal_char(op), payloadLit, mat_literal_char(note)); %#ok<AGROW>
end
end
% -------------------------------------------------------------------------
function s = payload_literal(p)
fns = fieldnames(p);
parts = cell(1, 2*numel(fns));
for k = 1:numel(fns)
f = fns{k};
v = p.(f);
parts{2*k-1} = ['''', f, ''''];
% Wrap every value in an extra {} so struct() stores it as a single
% scalar field. A bare cell value (e.g. a cellstr of column names)
% would otherwise make struct() build a struct ARRAY of that length,
% so downstream `payload.columnNames` expands to a comma-separated
% list and `isempty(payload.columnNames)` throws MATLAB:maxrhs. The
% {v} form unwraps one level, restoring the intended scalar field.
parts{2*k} = ['{', value_literal(v), '}'];
end
s = ['struct(', strjoin(parts, ', '), ')'];
end
% -------------------------------------------------------------------------
function s = value_literal(v)
if ischar(v)
s = mat_literal_char(v);
elseif isstring(v) && isscalar(v)
s = mat_literal_char(char(v));
elseif iscellstr(v) || isstring(v)
s = ['{', cellstr_literal_inner(cellstr(v)), '}'];
elseif isnumeric(v)
if isempty(v)
s = '[]';
elseif isscalar(v)
s = num2str(v);
else
s = ['[', num2str(reshape(v, 1, [])), ']'];
end
elseif isstruct(v)
s = payload_literal(v);
else
error('export_workflow_script:unsupportedLiteral', ...
'Cannot serialize value of class %s', class(v));
end
end
% -------------------------------------------------------------------------
function s = cellstr_literal_inner(c)
if isempty(c)
s = '';
return
end
parts = cell(1, numel(c));
for k = 1:numel(c)
parts{k} = mat_literal_char(c{k});
end
s = strjoin(parts, ', ');
end
% -------------------------------------------------------------------------
function lines = emit_resume_struct(resumeCycles)
lines = {};
lines{end+1} = 'resumeCycles = struct();';
fns = fieldnames(resumeCycles);
for k = 1:numel(fns)
lines{end+1} = sprintf('resumeCycles.%s = %d;', fns{k}, resumeCycles.(fns{k})); %#ok<AGROW>
end
end
% Copyright 2026 The MathWorks, Inc.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |