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.

2 views (last 30 days)
Hi, I have set of variables in 1x1 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

Accepted Answer

Cam Salzberger
Cam Salzberger on 7 Nov 2017
Edited: Cam Salzberger on 7 Nov 2017
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
  3 Comments
Cam Salzberger
Cam Salzberger on 8 Nov 2017
That still doesn't provide all the information you would need to know to do this. What is the "data". Is it a matrix? A vector? A scalar?
What does it represent? Temperature over the course of a period of time? Temperature in multiple locations?
What do you want the max/min/mean of? All temperature values in the entire struct? All temperature values in each "data" field of the struct? Some subset of each data field?
How do you want to store the output? In a cell array? In a structure? A scalar value?
At runtime, do you know the field names that contain the data you are interested in, or do you need to find them out programmatically? Are there any other fields in the struct that you are not interested in? How can you tell them apart if you have to determine the fields you are interested in programmatically?
For example, if I had a struct that I created in-program, and knew the size of the data, then I could simply access those fields:
% Example data
tempBuildingA = rand(10,1);
tempBuildingB = rand(10,1);
% Create the struct
S = struct('tBA', tempBuildingA, 'tBB', tempBuildingB);
% ...do other stuff...
% Extract temperature later and get stats
minTempA = min(S.tBA);
maxTempB = max(S.tBB);
meanTempBoth = mean([S.tBA ; S.tBB]);
That only works because I already know the fieldnames, shapes, and meanings behind all the data. You need to know how much you know before you can put that knowledge to use.
-Cam

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!