SQL to MATLAB Converter

Transform SQL database dumps into MATLAB-compatible formats including matrices, cell arrays, struct arrays, and table objects for data analysis and scientific computing

SQL Input

MATLAB Output

About SQL to MATLAB Converter

Convert SQL database dumps (CREATE TABLE and INSERT statements) to MATLAB-compatible formats. Supports matrices, cell arrays, struct arrays, and table objects for seamless integration into MATLAB data analysis, scientific computing, and visualization workflows.

Key Features

  • Multiple Formats: Matrix, cell array, struct array, and table (R2013b+)
  • Automatic Type Detection: Identifies numeric columns for optimal format
  • Variable Name Sanitization: Ensures MATLAB-compatible variable names
  • NaN Handling: Proper handling of NULL and missing values
  • Usage Examples: Optional comments with data access patterns
  • File Download: Save as .m file for direct use in MATLAB

How to Use

  1. Input SQL Data: Paste your SQL CREATE TABLE and INSERT statements or upload a .sql file
  2. Choose Format: Select matrix, cell array, struct array, or table format
  3. Configure Options: Toggle comments and usage examples
  4. Copy or Download: Use the Copy or Download button to save your MATLAB code
  5. Run in MATLAB: Execute the code in MATLAB or save as .m file

Output Formats

Table (Recommended, R2013b+):

  • Modern MATLAB table object with named variables
  • Supports mixed data types (numeric and string)
  • Easy variable access by name
  • Compatible with MATLAB's data analysis functions
  • Best for most use cases

Matrix (Numeric Only):

  • 2D numeric array format
  • Only includes numeric columns
  • Non-numeric columns replaced with NaN
  • Fast mathematical operations
  • Ideal for pure numeric data

Cell Array (Mixed Types):

  • Cell array with header row
  • Supports mixed data types
  • Access by row and column index
  • Compatible with all MATLAB versions
  • Good for heterogeneous data

Struct Array (Field-Based):

  • Array of structures with named fields
  • Record-oriented data access
  • Easy to work with individual records
  • Compatible with all MATLAB versions
  • Best for database-like operations

Example Conversion

SQL Input:

CREATE TABLE experiments (
  id INT,
  sample_name VARCHAR(50),
  temperature DECIMAL(5,2),
  pressure DECIMAL(6,2)
);

INSERT INTO experiments VALUES (1, 'Sample_A', 25.5, 101.3);
INSERT INTO experiments VALUES (2, 'Sample_B', 30.0, 98.7);

MATLAB Output (Table format):

% Table format (MATLAB R2013b and later)
% Rows: 2, Variables: 4

id = [1; 2];
sample_name = {'Sample_A'; 'Sample_B'};
temperature = [25.5; 30.0];
pressure = [101.3; 98.7];

experiments = table(id, sample_name, temperature, pressure);

% Access data:
% By variable name: experiments.id
% By row and column: experiments(1, :)
% By row and variable: experiments(1, 'id')

MATLAB Output (Matrix format):

% Numeric matrix format (only numeric columns)
% Rows: 2, Columns: 4

experiments = [
    1, NaN, 25.5, 101.3;
    2, NaN, 30.0, 98.7
];

% Column names:
% Column 1: id (numeric)
% Column 2: sample_name (non-numeric, replaced with NaN)
% Column 3: temperature (numeric)
% Column 4: pressure (numeric)

MATLAB Output (Struct format):

% Struct array format (field-based access)
% Records: 2

experiments(1).id = 1;
experiments(1).sample_name = 'Sample_A';
experiments(1).temperature = 25.5;
experiments(1).pressure = 101.3;

experiments(2).id = 2;
experiments(2).sample_name = 'Sample_B';
experiments(2).temperature = 30.0;
experiments(2).pressure = 98.7;

% Access data: experiments(index).fieldname
% Example: experiments(1).id
% Get all values for a field: [experiments.id]

Common Use Cases

  • Scientific Data Analysis: Import experimental data from databases
  • Machine Learning: Prepare datasets for MATLAB ML toolboxes
  • Signal Processing: Load time-series data for analysis
  • Statistics: Import data for statistical analysis
  • Visualization: Create plots and charts from database data
  • Simulation: Use database parameters in simulations

Data Type Handling

  • Numeric Values: Converted to MATLAB numeric types (integers, decimals)
  • Strings: Converted to MATLAB strings or cell arrays of strings
  • NULL Values: Converted to NaN (numeric) or empty strings (text)
  • Mixed Columns: Handled appropriately based on format choice

MATLAB Integration Examples

Table Operations:

% Filter rows
highTemp = experiments(experiments.temperature > 27, :);

% Calculate statistics
meanTemp = mean(experiments.temperature);

% Sort by column
sortedData = sortrows(experiments, 'temperature');

% Plot data
plot(experiments.id, experiments.temperature);

Matrix Operations:

% Extract numeric columns (skip NaN columns)
numericData = experiments(:, ~any(isnan(experiments)));

% Calculate correlation
corrMatrix = corrcoef(numericData);

% Perform linear regression
X = numericData(:, 1);
Y = numericData(:, 2);
coeffs = polyfit(X, Y, 1);

Struct Operations:

% Loop through records
for i = 1:length(experiments)
    fprintf('Sample: %s, Temp: %.1f\n', ...
        experiments(i).sample_name, experiments(i).temperature);
end

% Extract all temperatures
temps = [experiments.temperature];

% Filter by condition
highPressure = experiments([experiments.pressure] > 100);

Variable Name Sanitization

The converter automatically sanitizes column names to be MATLAB-compatible:

  • Replaces special characters with underscores
  • Ensures names start with a letter
  • Removes leading underscores
  • Handles numeric prefixes (e.g., "2nd_column" → "col_2nd_column")

MATLAB Version Compatibility

  • Table Format: Requires MATLAB R2013b or later
  • Matrix Format: Compatible with all MATLAB versions
  • Cell Array: Compatible with all MATLAB versions
  • Struct Array: Compatible with all MATLAB versions

Privacy & Security

All conversions happen locally in your browser. Your SQL data is never uploaded to any server, ensuring complete privacy and security.