Introduction: The Data Type Zoo of MATLAB
If you’ve spent any time with MATLAB, you’ve probably noticed that it has more data types than a Swiss Army knife has blades. After getting comfortable with basic matrices and arrays in the previous articles, you’re now ready to enter the fascinating world of heterogeneous data handling. This is where MATLAB transitions from being a pure numerical computing environment to a general-purpose data manipulation powerhouse.
Think about it: real-world engineering data is messy. Sensor logs have timestamps (strings), measurements (numbers), and status flags (booleans) all jumbled together. Experimental results come with metadata, units, and notes. Financial data has dates, prices, volumes, and text descriptions. None of this fits neatly into a simple numeric matrix, and that’s exactly why MATLAB gives us strings, cell arrays, structure arrays, and tables.
In this article, we’re going to dive deep into these data types. We’ll explore not just how to use them, but why they exist, how they’re implemented under the hood, and when to choose one over another. By the end, you’ll have a nuanced understanding that goes far beyond the basic syntax — you’ll understand the design philosophy behind MATLAB’s data type system and how to wield it effectively in real engineering applications.
Character Arrays vs String Arrays: A Tale of Two String Worlds
Let me start with a source of confusion that has plagued MATLAB users for decades: the dual string system. If you’ve ever wondered why sometimes strings use single quotes and sometimes double quotes, or why 'hello' and "hello" are different types entirely, you’re about to get some answers.
The Old World: Character Arrays (char)
Back in the early days of MATLAB (we’re talking the 1980s and 1990s), there was only one way to represent text: character arrays. A character array in MATLAB is essentially a row vector of characters, where each character is stored as a 16-bit Unicode value. When you type:
greeting = 'Hello, World!';
whos greeting
You’ll see that greeting is a 1×13 char array. It’s literally a matrix with one row and thirteen columns, where each element is a character. This becomes apparent when you try to index into it:
greeting(1:5)
% Returns: 'Hello'
% You can even do matrix operations!
greeting_matrix = ['Hello'; 'World'; 'MATLAB'];
% This creates a 3×5 char array
% Note: all rows must have the same number of columns!
This matrix-centric design is pure MATLAB philosophy — everything is a matrix, even text. But it leads to some quirks. For instance, if you want to store multiple strings of different lengths, you can’t just put them in an array:
% This will fail:
names = ['Alice'; 'Bob'; 'Christopher'];
% Error: Dimensions of arrays being concatenated are not consistent.
% The old workaround: use a cell array (more on this later)
names_cell = {'Alice'; 'Bob'; 'Christopher'};
Character arrays also have some behaviors that seem weird until you understand the implementation. Consider this:
a = 'MATLAB';
b = 'Rocks';
c = [a, b]; % Horizontal concatenation: 'MATLABRocks'
d = [a; b]; % Vertical concatenation: 2×6 char array
% But this fails:
e = [a; 'Hi']; % Error: dimensions don't match!
The character array system worked reasonably well for simple text processing, but it had limitations. You couldn’t easily have arrays of strings (as opposed to arrays of characters), string operations were often cryptic (strfind instead of contains), and the syntax was inconsistent with the rest of MATLAB.
The New World: String Arrays (string)
Enter MATLAB R2016b, a release that fundamentally changed how we work with text. MathWorks introduced the string data type, and with it, a whole new syntax using double quotes:
% The modern way
greeting = "Hello, World!";
whos greeting
% Now it's a 1×1 string (not char!)
% Create an array of strings
names = ["Alice", "Bob", "Christopher"];
% This is a 1×3 string array, not a char array!
This might seem like a minor syntactic difference — single quotes vs double quotes — but it represents a fundamental shift in how MATLAB thinks about text. A string array is its own data type, not a char array in disguise. This gives us superpowers:
% String arrays can have different lengths in each element
names = ["Alice", "Bob", "Christopher"];
lengths = strlength(names);
% Returns: [5, 3, 11]
% You can use familiar array operations
names(1) = "Alicia"; % Index and assign like any other array
names(end+1) = "David"; % Append like any other array
% String comparisons use natural operators
if names(1) == "Alice"
disp('Found Alice!');
end
% Try that with char arrays and you'll get an error about matrix dimensions!
But here’s the thing that confuses people: both char and string still exist in modern MATLAB. The language maintains backward compatibility, so old code using char arrays still works. This means you’ll encounter both in the wild, and you need to know how to work with both.
Conversion Between char and string
In practice, you’ll often need to convert between these types. Maybe you’re working with old code that returns char arrays, or maybe you need to interface with a function that only accepts string arrays. Here’s your conversion cheat sheet:
% char to string
text_char = 'Hello World';
text_string = string(text_char);
% Or simply:
text_string = "Hello World";
% string to char (only works for scalar strings!)
text_string = "Hello World";
text_char = char(text_string);
% But what if you have a string ARRAY?
names = ["Alice", "Bob", "Charlie"];
% char(names) will fail if the strings have different lengths!
% Solution: use cell arrays as an intermediate
names_cell = cellstr(names);
% Now names_cell = {'Alice', 'Bob', 'Charlie'}
There’s also the convertStringsToChars function, which is particularly useful when writing functions that need to accept both string and char inputs:
function processText(textInput)
% Accept both string and char input
textInput = convertStringsToChars(textInput);
% Now textInput is guaranteed to be char (or cellstr for arrays)
% ... rest of function ...
end
Performance Implications
Now, let’s talk about something that doesn’t get enough attention: the performance characteristics of char vs string. This is the kind of deep analysis that separates the casual users from the power users.
Character arrays are essentially matrices of 16-bit integers (Unicode code points). When you manipulate them, MATLAB is doing relatively simple matrix operations. String arrays, on the other hand, are objects with their own methods and properties. Each string element carries metadata and supports a richer set of operations.
What does this mean in practice?
% Timing comparison
n = 1000000;
% Creating char array
tic;
char_array = repmat('x', 1, n);
char_time = toc;
% Creating string
tic;
string_array = repmat("x", 1, n);
string_time = toc;
fprintf('char: %.4f seconds\n', char_time);
fprintf('string: %.4f seconds\n', string_time);
In general, char arrays are faster for simple operations and use less memory. A char array stores only the characters (2 bytes per element). A string array stores references to string objects, which have overhead.
However — and this is important — string arrays shine when you’re doing complex text processing. Functions like contains, extract, and replace are optimized for string arrays and can be faster than their char-based counterparts (strfind, strrep, etc.) for many operations.
Rule of thumb: Use char for simple storage and when interfacing with old code. Use string for text processing, when you need string arrays (as opposed to character arrays), and when writing new code.



