Any function to get a short description of a variable just like the Value column in workspace?.

2 ビュー (過去 30 日間)
I have a struct with >4000 members/fields. I am writing a search function to find some matching members and displaying them along with a short description. Ideally i want the description to be like the value column of workspace(see below).
  2 件のコメント
Von Duesenberg
Von Duesenberg 2018 年 4 月 25 日
編集済み: Von Duesenberg 2018 年 4 月 25 日
If your structure is called s for example, you can access the size of fields like this (so I suppose you can access other properties as well):
structfun(@size, s, 'Uni', 0)
Praveen Iyyappan Valsala
Praveen Iyyappan Valsala 2018 年 4 月 25 日
Thanks for the suggestion, but I don't need just the size:(. I need value if it is a short array/single element or its size if it is a long one. Also the input can be another struct, class object or other datatype.

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

回答 (1 件)

Jan
Jan 2018 年 4 月 25 日
編集済み: Jan 2018 年 4 月 25 日
What about writing a function, which satisfies your needs exactly?
function C = DispStructRoughly(S)
Field = fieldnames(S);
Data = struct2cell(S);
C = cell(1, numel(Field));
for iC = 1:numel(Field)
D = Data{iC};
classD = class(D);
if ndims(D) > 3
if isnumeric(D) && ~isreal(D)
comp = 'complex ';
else
comp = '';
end
if isempty(D)
empty = 'Empty ';
else
empty = '';
end
C{iC} = sprintf('%s%d dim %s%s array', empty, ndims(D), comp, classD);
elseif isnumeric(D)
...
elseif ischar(D)
C{iC} = D;
elseif ...
end
end
It is not hard to create the wanted strings exactly as you need them with all exceptions as you like. Simply try to expand this code and ask a specific question in case of problems.
  2 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 4 月 25 日
Just wondering, if there is a way to directly access these strings. MATLAB must have stored them in variable metadata, for display purposes.
Praveen Iyyappan Valsala
Praveen Iyyappan Valsala 2018 年 4 月 25 日
No wonder you are MVP;). I also thought of writing a function but then there are too many conditions and I got lazy:P . I tried some convoluted ways like this using details() / disp() and followed some string operation:
raw_text =evalc('details(MyStruct.parameter);');
This works for most except numeric arrays. where it prints all the elements.

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

カテゴリ

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