Table with multiple types

25 views (last 30 days)
Gavin
Gavin on 17 Nov 2025 at 22:21
Answered: Umar on 17 Nov 2025 at 23:58
I'm trying to use a ui table of 2 columns, one of chars, and the other uint32. Seems like it used to work. Now it fails badly and the workaround to make PType a string is not what I want (makes table of strings). Why can't a table have multipe types as assigned?
Example 1 Try in Command window
PType =
'R'
K>> PData
PData =
391
Start with
app.LicksTable.Data = [];
Too late ML already decided what the columns are to be (strings in this case)
K>> app.LicksTable.Data = [PType,PData];
Error setting property 'Data' of class 'Table':
Data must be a numeric, logical, string, cell, or table array
Example 3 What happens in my code when I've done PType = convertCharsToStrings(char(PType));
if isempty(app.LicksTable.Data) % Init (sets the var types too)
app.LicksTable.Data = [PType, PData];
else
app.LicksTable.Data(end+1,:) = [PType, PData];
end
K>> app.LicksTable.Data
ans =
1×2 string array
"R" "391"
When I tried making it all numeric, then I lose the data (PData still uint32)
K>> PType = uint8(char(PType))
PType =
76
K>> app.LicksTable.Data = [PType, PData];
K>> app.LicksTable.Data
ans =
1×2 uint8 row vector
76 255
More things to try: (PType a char again)
>> app.LicksTable.Data = table (PType,PData);
Error using table (line 87)
Invalid parameter name: L.
Caused by:
You might have intended to create a one-row table with the character vector 'L' as one of its variables. To store text data in a table, use a string array or a cell array of character vectors rather than character arrays. Alternatively, create a cell array with one row, and convert that to a table using CELL2TABLE.
Well at least that's helpful
K>> PType = convertCharsToStrings(char(PType));
K>> app.LicksTable.Data = [];
K>> app.LicksTable.Data = table (PType,PData);
K>> app.LicksTable.Data
ans =
1×2 table
PType PData
_____ _____
"L" 372
Wow I got it!
For a language that claims to be weakly typed it sure knows how to make a mess!
I'm going to post this anyway in case anyone else is having similar trouble with chars and strings in tables with numbers.

Answers (3)

Matt J
Matt J on 17 Nov 2025 at 22:49
Edited: Matt J on 17 Nov 2025 at 23:28
I guess I might be missing something. Why isn't this what you want?
PType='R';
PData=uint32(391);
fig = uifigure;
uit = uitable(fig,"Data",{ PType, PData})
uit =
Table with properties:
Data: {'R' [391]}
ColumnWidth: 'auto'
ColumnEditable: []
CellEditCallback: ''
Position: [20 20 300 300]
Units: 'pixels'
Show all properties

Walter Roberson
Walter Roberson on 17 Nov 2025 at 23:03
PType = 'R';
PData = uint32(391);
app.LicksTable.Data = [PType,PData]
app = struct with fields:
LicksTable: [1×1 struct]
app.LicksTable.Data
ans = 'RƇ'
The [] operation between a char and a uint32, is going to result in a single char array. That single char array is then the wrong size to populate the two-variable uitable.
You would have been better off using
app.LicksTable.Data = {PType,PData}

Umar
Umar on 17 Nov 2025 at 23:58

@Gavin and @Matt J,

I also wanted to share some documentation insights that might help clarify this mixed-type uitable issue.

To Gavin: You correctly identified that the table() constructor interprets char vectors as parameter names, which is why string conversion was necessary. However, for iterative row additions like yours, cell array syntax is more efficient:

   app.LicksTable.Data(end+1,:) = {PType, PData};

This eliminates both the char/string conversion step and the repeated table array construction overhead.

To Matt J: Your cell array solution is exactly the documented approach. For others reading this thread, it extends naturally to multiple rows:

uit.Data = {PType1, PData1; PType2, PData2; PType3, PData3};

Bottom line: For mixed-type uitable data, cell arrays are simpler and more performant than table array construction, especially when adding rows programmatically.

Hope this helps!

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!