Visualize data in App Designer similarly to Variable Editor
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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)
% 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))
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
