Visualize data in App Designer similarly to Variable Editor

11 visualizaciones (últimos 30 días)
Vittorio
Vittorio el 8 de Abr. de 2021
Editada: Adam Danz el 14 de Abr. de 2021
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 comentarios
Vittorio
Vittorio el 8 de Abr. de 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 el 8 de Abr. de 2021
Editada: Adam Danz el 12 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 8 de Abr. de 2021
Editada: Adam Danz el 14 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by