Using methods across multiple class objects of the same class?
22 views (last 30 days)
Show older comments
Hello everyone
In short: What is the best way to implement a method that uses the properties of multiple objects of the same class?
In long: I have multiple sets of data from experiments that I would like to analyze. Experiments happen at certain operating conditions with minor changes across individual experiment runs, with a run meaning to take N measurements at M locations over P seconds. So the data can be split according to operating point (OP), and each operating point according to run (R). Analysis with Matlab should happen on both levels.
My current approach is to collect and evaluate the R-data in a custom class, with the relevant pseudo-code:
%in ..\Project\@RunData\RunData.m
classdef RunData
properties
% various properties like: data, information about experiment, e.g., sampling rate and signal length
fooProp1
fooProp2
% ...
end
methods
function obj=RunData(fooFile)
% Constructor
if nargin==0
%some properties=NaN
end
% initialize some other properties
[fooProp1, fooProp2]=load(fooFile);
end
function testPrint(obj)
fprintf('Value of foo is %s\n',exampleProp1)
end
% ...
end
end
Now here was the main problem: I want to combine foo1 of multiple objects of class RunData. Let's say, as an example, that I want to get the average of foo1 across all objects.
Thus, my first question: Is it possible to define a function, e.g. "calculateAverage", within the class methods that would evaluate the property combined across each object of that class?
--> Depending on how the question above is answered, the rest below might be completely irrelevant.
Tinkering around with the code and gathering resources from various sources online, I then tried creating an "enveloping class" on OP-level that would hold all the individual runs, on which I could run "array-functions", e.g., get the average of fooProp1 across all runs, with the pseudo-code:
%in ..\Project\@OperatingPoint\OperatingPoint.m
classdef OperatingPoint
properties
Run RunData % An array comprised of RunData elements
fooProp3
fooProp4
% ...
end
methods
function obj=OperatingPoint(fooFolder)
% Constructor that creates an array entry for each data file
listOfFiles=getDataFilesInFolder(fooFolder)
for i=1:NumberOfDataFiles
obj.Run(i)=RunData(listOfFiles(i))
end
end
end
end
Onto the second question: Is there a more elegant approach to the problem than the one I described above?
And finally, as my third question: Is it possible to define the set/get methods so that the call:
>> MyOperatingPoint.Run.fooProp1
would result in an array of fooProp1 of all runs that are in the variable MyOperatingPoint instead of Matlab iterating over all runs and displaying fooProp1 of each individual run?
For anyone that has read this far, thank you very much for your patience!
3 Comments
Accepted Answer
Image Analyst
on 21 Aug 2023
If the results are strings, try
{MyOperatingPoint.Run.fooProp1}
or
vertcat(MyOperatingPoint.Run.fooProp1)
More Answers (0)
See Also
Categories
Find more on Whos 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!