Real-World MATLAB/Octave Support
This document describes the comprehensive MATLAB/Octave compatibility features that have been added to the XDL transpiler to support real-world scientific computing code.
Overview
The XDL MATLAB transpiler now supports a wide range of real-world MATLAB patterns commonly found in scientific computing, data analysis, and visualization code.
Features Implemented
1. Range Operators in Expressions
MATLAB Code:
t = (0:L-1)*T; % Create range from 0 to L-1, multiply by T
x = (1:2:10); % Create range with step: 1, 3, 5, 7, 9
XDL Equivalent:
t = FINDGEN(L) * T
x = (FINDGEN(((10)-(1))/(2) +1) * (2)) + (1)
How It Works:
- The transpiler detects colon operators
:inside parentheses - Converts
(start:end)toFINDGEN((end)-(start)+1) + (start) - Optimizes
(0:N-1)to simplyFINDGEN(N) - Handles step ranges
(start:step:end)appropriately
2. Array Slicing with Ranges
MATLAB Code:
arr = (0:19);
slice = arr(1:50); % Get elements 1 through 50
subset = arr(5:10); % Get elements 5 through 10
How It Works:
- Array indexing with range expressions
arr(start:end)is fully supported - Automatically converts MATLAB’s 1-based indexing to XDL’s 0-based indexing
- Handles both numeric indices and variable-based ranges
3. Random Number Generation
MATLAB Code:
r = rand(size(t)); % Uniform random with same size as t
r = randn(size(data)); % Normal random with same size as data
r = rand(10, 20); % Uniform 10x20 array (falls back to RANDOMU)
XDL Equivalent:
r = RANDOMU(1, N_ELEMENTS(t))
r = RANDOMU(1, N_ELEMENTS(data))
r = RANDOMU(1, 10, 20)
How It Works:
- Special handling for
rand(size(x))andrandn(size(x))patterns - Converts
size(x)toN_ELEMENTS(x)for RANDOMU/RANDOMN - Both
randandrandncurrently map toRANDOMU(normal distribution to be added later) - Seed is fixed at 1 for reproducibility
4. Statistical Functions
Supported Functions:
mean(x)→MEAN(x)std(x)→STDDEV(x)min(x)→MIN(x)max(x)→MAX(x)sum(x)→TOTAL(x)median(x)→MEDIAN(x)var(x)→VARIANCE(x)
5. Mathematical Functions with Arrays
Element-wise Operations:
X = A .* B; % Element-wise multiply
Y = A ./ B; % Element-wise divide
Z = A .^ B; % Element-wise power
Array Functions:
y = sin(x); % Sine (operates on arrays)
y = cos(x); % Cosine
y = exp(x); % Exponential
y = sqrt(x); % Square root
y = abs(x); % Absolute value
y = log(x); % Natural logarithm
All math functions work seamlessly with both scalar and array inputs.
6. Advanced Plotting
Line Styles:
plot(x, y, 'b-'); % Blue solid line
plot(x, y, 'r--'); % Red dashed line
plot(x, y, 'g:'); % Green dotted line
Line style strings are detected and gracefully ignored (XDL doesn’t support line styles yet).
Multiple Plots:
figure;
plot(x, y1, 'b-');
hold on;
plot(x, y2, 'r--');
hold off;
title('Multiple Plots');
xlabel('X axis');
ylabel('Y axis');
Tiled Layouts:
tiledlayout(2, 2);
nexttile;
plot(x1, y1);
nexttile;
plot(x2, y2);
Converts to tile-specific plot files: tile1_plot.png, tile2_plot.png, etc.
3D Plots:
comet3(x, y, z); % 3D animated comet plot
plot3(x, y, z); % 3D line plot
Converts to PLOT3D with appropriate filenames.
7. Constants
Built-in Constants:
pi→!PIe→!E
Example:
x = linspace(0, 2*pi, 100);
y = sin(x);
Transpiles correctly with pi mapped to !PI.
Tested Real-World Examples
Example 1: Noisy Signal Analysis
% Generate sample data
n = 100;
t = (0:n-1) * 0.1;
data = sin(2*pi*0.5*t) + 0.5*randn(size(t));
% Basic statistics
data_mean = mean(data);
data_std = std(data);
% Plot
figure;
plot(t, data, 'b-');
title('Noisy Sine Wave');
✅ Works perfectly
Example 2: Multi-panel Plots
tiledlayout(2, 2);
nexttile;
x1 = (0:99) * 0.1;
plot(x1, sin(x1));
title('Sine');
nexttile;
x2 = (0:99) * 0.1;
plot(x2, cos(x2));
title('Cosine');
✅ Works perfectly - generates separate plot files for each tile
Example 3: Array Operations
x = (0:99) * 0.1; % Range with scaling
y = sin(x); % Function on array
z = y(10:50); % Array slicing
✅ Works perfectly
Limitations and Future Work
Currently Not Supported
-
Multiple Return Values:
[X, Y] = meshgrid(-5:0.25:5); % Not supported -
Anonymous Functions:
f = @(x) x^2 + 2*x + 1; % Not supported -
ODE Solvers:
[t, y] = ode45(@func, [0 10], [1 1 1]); % Not supported -
FFT and Advanced Signal Processing:
Y = fft(X); % FFT function exists but complex number handling needs work -
Surface/Mesh Plots:
surf(X, Y, Z); % Needs surface plotting support meshgrid(...); % Needs 2D array generation -
Matrix Operations:
- Matrix multiplication (non-element-wise)
- Linear algebra functions (inv, det, eig, etc.)
Planned Improvements
- Normal Distribution Random Numbers:
- Add proper
RANDOMNfunction to XDL stdlib - Currently both
randandrandnuseRANDOMU
- Add proper
- Array Literals:
- Support for
[1, 2, 3]syntax - Multi-dimensional arrays
- Support for
- Control Flow:
if/else/elseifstatementsforandwhileloopsswitch/casestatements
- Functions and Scripts:
- Function definitions
- Multiple file support
- Path management
Usage Examples
Running a MATLAB File
xdl my_matlab_script.m
The transpiler automatically detects .m files and transpiles them to XDL before execution.
Example Session
$ cat example.m
% Simple analysis
x = (0:99) * 0.01;
y = sin(2*pi*5*x);
plot(x, y);
$ xdl example.m
INFO: Detected MATLAB .m file, transpiling to XDL
PLOT: Rendering 100 points to xdl_plot.png
Plot saved to 'xdl_plot.png'
Testing
Run the comprehensive test suite:
xdl /tmp/comprehensive_matlab_test.m
This tests:
- Range operators (0:9, 1:2:10)
- Array slicing
- Random number generation
- Statistical functions
- Mathematical functions on arrays
- Plotting with line styles and hold on/off
Conclusion
The XDL MATLAB transpiler now supports a substantial subset of real-world MATLAB code, particularly for:
- Numerical computation with arrays
- Statistical analysis
- Signal generation and processing
- 2D and 3D plotting
- Multi-panel visualizations
This makes it practical for transpiling many scientific computing scripts from MATLAB/Octave to XDL/IDL.