MATLAB: Hi, I have set of variables in <1*1 struct> class. Each variables having a data. Is there a way to find out the statistics for each variables which are in struct classes.

statistics for struct classes

Hi, I have set of variables in 1×1 struct class. Each variables having a data. Is there a way to find out the statistics for each variables which are in struct classes.
Thanks

Best Answer

Hello Sandeep,
It's not clear what you mean by "statistics", nor what you know about each field of the struct. However, if I were to assume you had something like this:
S = struct('A', rand(3), 'X', rand(4));
so all data in every field in the structure is numeric, and I wanted to compute the size of each matrix, I could do something like:
sizeStruct = struct;
fields = fieldnames(S); % Get all the fields to loop through
for k = 1:numel(fields) % Loop through each field
f = fields{k}; % Name of the field
sizeStruct.(f) = size(S.(f)); % Use dynamic field names
end
If you want to avoid a for loop, you can get fancy with cellfun:
S = struct('A', rand(3), 'X', rand(4));
fields = fieldnames(S);
sizeCell = cellfun(@(f) size(S.(f)), fields, 'UniformOutput', false);
Now you have all the data in a cell array, in the same order as the fields cell array. If you want it back into a struct with the same fields as S, you could do the same thing with cellfun, or get fancy with indexing:
C = [fields.' ; sizeCell.'];
sizeStruct = struct(C{:});
Is this the kind of thing you're looking for? If not, what "statistics" do you mean, and what kind of data is contained within the struct?
-Cam
Related Question