Visualize data in App Designer similarly to Variable Editor

조회 수: 2 (최근 30일)
Vittorio
Vittorio 2021년 4월 8일
편집: Adam Danz 2021년 4월 14일
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
Vittorio
Vittorio 2021년 4월 8일
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 2021년 4월 8일
편집: Adam Danz 2021년 4월 12일
> 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 4월 8일
편집: Adam Danz 2021년 4월 14일
> 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by