フィルターのクリア

Can you set struct to always display dimensions instead of field values?

20 ビュー (過去 30 日間)
Nick Nidzieko
Nick Nidzieko 2022 年 3 月 12 日
回答済み: bubbleguy 2022 年 3 月 18 日
In prior Releases, the default command line display for a structure was to show the array size. Here state is a struct with three fields.
>> state
ans =
struct with fields:
mission_number: [1x13549 double]
software_revision: [1x13549 double]
vehicle_temperature: [1x13549 double]
In the latest release, the values of the arrays are shown instead.
>> state
ans =
struct with fields:
mission_number: [0 0 0 0 0 ... ]
software_revision: [22 22 22 ... ]
vehicle_temperature: [28.9411 ... ]
I want a quick way to look at the sizes of my structs and the arrays within those structs. I don't use the MATLAB GUI, do please don't tell me to look in the workspace Variables panel... You can force the old (more useful!) method if the window is very narrow.
Is there a way to force the struct output to only use the old method?
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 12 日
In prior Releases, the default command line display for a structure was to show the array size.
Not completely true. When the field only had a small number of elements, the contents were shown. If I recall correctly, 4 elements was the usual limit.

サインインしてコメントする。

回答 (3 件)

bubbleguy
bubbleguy 2022 年 3 月 18 日
I just updated Matlab to 2022a and I found the new feature that Nick Nidzieko described is very annoying. For most of people, knowing the dimension of a struct field is much more important and valuable than knowing a few values of the field. I hope Matlab can reverse back this feature.

Voss
Voss 2022 年 3 月 12 日
I don't know if there's a setting to control that, but you can always write your own function to do it.
Here's one you might try out, which will display the size and class of each field of a scalar struct:
s = struct( ...
'mission_number',randn(1,13549), ... % using fields of various sizes here
'software_revision',randn(1,149), ...
'vehicle_temperature',randn(1,3549));
struct_disp(s);
mission_number: [1x13549 double] software_revision: [1x149 double] vehicle_temperature: [1x3549 double]
s.new_cell_field = repmat({[1 2 3]},100,42,13); % include a 3D cell array in s
struct_disp(s);
mission_number: [1x13549 double] software_revision: [1x149 double] vehicle_temperature: [1x3549 double] new_cell_field: [100x42x13 cell]
function struct_disp(s)
f = fieldnames(s);
nf = max(cellfun(@numel,f))+4;
fprintf_format = sprintf('%%%ds: [%%s %%s]\n',nf);
for ii = 1:numel(f)
size_str = sprintf('%dx',size(s.(f{ii})));
size_str(end) = [];
fprintf(1,fprintf_format,f{ii},size_str,class(s.(f{ii})));
end
end

Walter Roberson
Walter Roberson 2022 年 3 月 12 日
If you were willing to wrap the struct within a class, then you could customize the display format; see https://www.mathworks.com/help/matlab/matlab_oop/custom-display-interface.html
Wrapping within a class is not always convenient, so the idea of creating your own display function may make more sense, as shown in https://www.mathworks.com/matlabcentral/answers/1670244-can-you-set-struct-to-always-display-dimensions-instead-of-field-values#answer_916144

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by