Visualize data in App Designer similarly to Variable Editor

10 views (last 30 days)
I have an App Designer app which needs to visualize to the user the contents of a struct. The data in the struct are a variety or numeric, string, datetime, etc. I can easily use struct2table and show that data in a UITable, but the fields are used as variable names, i.e. columns: since I have a lot of fields in the struct, this results in a table with one row and many many columns which force the user to scroll left and right to see them all. I cannot "transpose" the table, since I cannot have different data types for a variable in different rows. For now I am working around it by converting all the different data types to string, and then assembling them into a ListBox, which the user can scroll up or down.
The really ideal solution would be to visualize a struct the same way the variable editor does, with all the fields listed as "rows", and their value next to them, in whichever format they natively are.
Is there any way to mimic that behavior using a table? Or using some other UI object?
Thanks!
  5 Comments
Vittorio
Vittorio on 8 Apr 2021
I'm not sure what the difference between a variable of class table and the uitable is for my problem, since I end up storing the table into UITable.Data, so if I cannot do something for the table, I cannot then enter it in uitable.
Yes, if I converted to string everything would work, because then all rows would be the same data type. But I cannot visualize the variables in their native format, be it a datetime, or numeric, or others.
Adam Danz
Adam Danz on 8 Apr 2021
Edited: Adam Danz on 12 Apr 2021
> I'm not sure what the difference between a variable of class table and the uitable
A table is organized such that the variables are columns and observations are rows.
A uitable is a 2D gridded graphics object that you can organize in just about any way you can imagine.
See my answer below for an example.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 8 Apr 2021
Edited: Adam Danz on 14 Apr 2021
> The really ideal solution would be to visualize a struct the same way the variable editor does, with all the fields listed as "rows", and their value next to them
Demo: Create a structure with numbers, datetimes, strings, and categorical values.
The structure is conerted to a cell array for easier access.
Currently (r21a) uitables accept numeric array | logical array | cell array and if you're using a uifigure, string arrays. Other data types need to be converted. For simplicity I've converted all of the non character classes to character vectors.
The converted values are assigned to a uitable and the structure field names are used as row names in the table.
S = struct('a', rand(1), 'b', datetime('today'), 'c', "matlab", 'd', categorical({'apple'}), ...
'e', "MathWorks", 'f', categorical({'banana'}), 'g', datetime('now'),'h',pi)
S = struct with fields:
a: 0.5093 b: 08-Apr-2021 c: "matlab" d: apple e: "MathWorks" f: banana g: 08-Apr-2021 18:22:16 h: 3.1416
% Convert to cell array
Sdata = struct2cell(S);
% Convert strings to chars
Sdata(cellfun(@isstring, Sdata)) = cellstr(Sdata(cellfun(@isstring, Sdata)));
% Convert categoricals to chars
Sdata(cellfun(@iscategorical, Sdata)) = cellstr([Sdata{cellfun(@iscategorical, Sdata)}]);
% Convert datetimes to chars
Sdata(cellfun(@isdatetime, Sdata)) = cellstr([Sdata{cellfun(@isdatetime, Sdata)}]);
% Convert numbers to char (OPTIONAL, but good for consistent text justification)
Sdata(cellfun(@isnumeric, Sdata)) = strsplit(num2str([Sdata{cellfun(@isnumeric, Sdata)}]));
% Create UITable
% Row names are field names
uitable('data', Sdata,'RowName',fields(S))
ans =
Table with properties: Data: {8×1 cell} ColumnWidth: 'auto' ColumnEditable: [] CellEditCallback: '' Position: [20 20 300 300] Units: 'pixels' Show all properties

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!