MATLAB Transpiler Limitations
Overview
The XDL MATLAB transpiler provides basic MATLAB compatibility but has several limitations. This document outlines what works and what doesn’t.
What Works ✅
Basic Operations
- ✅ Scalar arithmetic:
x = 5 + 3; - ✅ Variable assignment:
a = 10; - ✅ Comments:
% This is a comment - ✅ Basic operators:
+, -, *, /, ^
Mathematical Functions
- ✅ Trigonometric:
sin(), cos(), tan(), asin(), acos(), atan() - ✅ Exponential/Log:
exp(), sqrt() - ✅ Natural log:
log()→ transpiles toALOG() - ✅ Base-10 log:
log10()→ transpiles toALOG10() - ✅ Other:
abs(), floor(), ceil(), round()
I/O Functions
- ✅ Display output:
disp(value)→ transpiles toPRINT, value - ✅ Nested calls:
disp(sin(0.5))works correctly
Control Flow
- ✅ IF/ELSE/ENDIF statements
- ✅ FOR loops with ranges
- ✅ WHILE loops
- ✅ SWITCH/CASE statements
- ✅ BREAK and CONTINUE statements
What Doesn’t Work ❌
Array/Matrix Operations
- ❌ Array literals:
[1, 2, 3, 4] - ❌ Matrix literals:
[1 2; 3 4] - ❌ Element-wise operations on arrays:
y = sin(x)where x is array - ❌ Array indexing with 1-based indexing
- ❌ Colon operator:
1:10,1:2:10
Array Generation Functions
- ✅
linspace(start, end, n)→ transpiles toLINSPACE() - ✅
logspace()→ transpiles toLOGSPACE() - ⚠️
zeros(),ones()- UseREPLICATE(0, n)orDBLARR(n)instead - ✅
eye()→ transpiles toIDENTITY() - ✅
rand(),randn()→ transpiles toRANDOMU(),RANDOMN()
Plotting Features
- ❌
figure- window management - ❌
hold on/off- multiple plots - ❌
xlabel,ylabel- axis labels (use XDL keywords instead) - ❌
title- plot title (use XDL keywords instead) - ❌
legend- plot legend - ❌ Line styles:
'b-','r--', etc. - ❌ Markers:
'*','o', etc. - ❌ Plot properties:
LineWidth,Marker, etc.
Advanced Features
- ❌ User-defined functions
- ❌ Anonymous functions/lambda:
@(x) x^2 - ❌ Cell arrays:
{1, 2, 'three'} - ❌ Structures:
struct.field - ❌ Classes and objects
- ❌ String operations beyond basic strings
- ❌ File I/O:
fopen,fread,fwrite, etc. - ❌
fprintfwith format strings
Control Flow Limitations
- ✅
switch/casestatements - Now supported - ❌
try/catcherror handling - Not yet implemented - ✅ Complex FOR loop ranges:
for i = 1:2:10- Supported - ✅
whileloops - Now supported - ✅
break,continue- Now supported
Workarounds
Instead of Array Literals
MATLAB:
x = [1, 2, 3, 4, 5];
XDL equivalent (write directly in XDL):
x = FINDGEN(5) + 1 ; Creates [1, 2, 3, 4, 5]
Instead of linspace
MATLAB:
x = linspace(0, 2*pi, 100);
XDL equivalent:
x = FINDGEN(100) * 2.0 * !PI / 99.0
Instead of MATLAB Plotting
MATLAB:
plot(x, y);
xlabel('X Axis');
ylabel('Y Axis');
title('My Plot');
XDL equivalent:
PLOT, x, y, title='My Plot', xtitle='X Axis', ytitle='Y Axis'
For Constants
MATLAB:
x = 2 * pi;
Use XDL system variables:
x = 2 * !PI
Available constants: !PI, !E, !DTOR (degrees to radians), !RTOD (radians to degrees)
Recommended Approach
For best results with MATLAB files:
- Simple Scalar Math: Use for basic calculations
a = 5; b = 10; c = sin(a) + cos(b); disp(c); - Individual Function Calls: Test mathematical functions
x = 0.5; y = sin(x); disp(y); -
Avoid Complex Features: Don’t use arrays, loops, or advanced plotting
- For Complex Work: Write directly in XDL
- Full array support
- Proper FOR loops
- Complete plotting with keywords
- All XDL features available
Migration Strategy
If you have MATLAB code to migrate:
- Simple scripts: May work with transpiler (test first)
- Array operations: Rewrite in XDL using FINDGEN, FLTARR, etc.
- Plotting: Rewrite using XDL PLOT with keywords
- Complex logic: Rewrite in XDL syntax
Examples That Work
See examples/matlab/ for working examples:
01_simple_math.m- Basic arithmetic ✅02_trigonometry.m- Trig functions ✅03_simple_operations.m- sqrt, exp, log ✅test_gui_output.m- Output capture ✅
Recent Improvements ✅
Enhancements completed in 2025:
- ✅
linspace()andlogspace()functions - ✅ Better FOR loop handling with ranges
- ✅ WHILE loop support
- ✅ SWITCH/CASE statement support
- ✅ BREAK and CONTINUE statements
- ✅ More complete function mapping (~80 functions)
Future Improvements
Planned enhancements:
- Array literal support:
[1, 2, 3] try/catcherror handling- Plot style parsing (convert to XDL equivalents)
- Low-level file I/O (
fopen,fread,fwrite)
Getting Help
If you encounter issues:
- Check this document for known limitations
- Try rewriting in XDL syntax
- See
examples/xdl/for XDL examples - Consult
docs/MATLAB_SUPPORT.mdfor supported features