MATLAB Answers

# How to design a Class to apply operations for a collection of data?

조회 수: 1(최근 30일)
zhehao.nkd 2021년 4월 9일
댓글: zhehao.nkd 2021년 4월 12일
For example, I have two collections of data, audio stimuli and neurons' response to these audios. What I need is to (1) calculate some values for each audio ( e.g. fs,y, spectrogram) or each neuron's response and (2) calculate other values based on the whole collections of audios or all neurons' response ( e.g. find the audio file which has the maximum average volume) and finally (3) aggregate all these values from audios and neural responses together for more complex calculations.(e.g. fitting the stimulus-response function).
Basically I plan to design three Classes, Class Audio for analyzing audios, Class Neuron for analyzing neural response, and Class Aggregation which aggregate the first two classes and do complex calculations. My question is, should I create a Class that processes only a single audio/neuron or a class that processes all audios/neurons at once? Or is there any better way to design such a structure?
I found both ways have some weaknesses. If the Class is for a single audio/neuron, I have to write functions outside this Class to calculate values based on the whole audios/neurons collection.
classdef Audio
properties
y
fs
end
methods
function obj = Audio(path)
[y,fs] = audioread(path);
end
%...
end
end
function maxvolume = maxVolume(path_collection)
for path = 1: length(path_collection)
% iterate for each path
end
% calculations based on the whole collection of data
end
While If the Class is for collections of audio files, then I may have to write iterations in each methods, especially for the class Aggregation:
classdef Aggregation
%...
function f1(audios, neurons)
for m = 1: length(neurons)
for n = 1: length(audios)
... detailed calculations
end
end
end
function f2(audios, neurons)
for m = 1: length(neurons)
for n = 1: length(audios)
... detailed calculations
end
end
end
%... same for other functions
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jeff Miller 2021년 4월 10일
This is perhaps a bit subjective, but personally I would strongly prefer the solution with classes that process only a single audio/neuron. Small, highly focussed classes are easier to use for unanticipated future purposes,
"If the Class is for a single audio/neuron, I have to write functions outside this Class to calculate values based on the whole audios/neurons collection." This does not sound like a disadvantage to me. A function to process a collection should be outside the class defining items of that collection, since that function is not applicable with a single instance. You could make a new class for collection processing if that seems useful, but from your description it just sounds to me like this collection processor should be a stand-alone function.
##### 댓글 수: 1표시숨기기 없음
zhehao.nkd 2021년 4월 12일
Thank you very much! I tried coding a class that process a single audio/neuron. This method works well, and it is convenient to add new methods to the class.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by