Designing a container to store a structure
Show older comments
I am working with signals, i have a class where a signal is chopped into multiple parts.
Test script:
filname = load('file');
signal = filname.xvalue
time = filname.tvalue;
f = ltiFilter.datahandle();
segment = f.addsig(signal,time);
Datahandle is a class where addsig is my function which does the chopping.
Datahandle: Not so important, just understand, this addsig function returns a structure with chopped signals
classdef datahandle
properties
test = struct();
end
methods
function y = addsig(this,s,ti)
%chops the signal into N number of small segments, based on the
%peak theshold of the difference of the time vector
interval = diff(ti);
[value,ind] = findpeaks(interval,'THRESHOLD',0.7);
if length(ind) == 0
this.test(1).y = s;
this.test(1).t =ti;
elseif length(ind) == 1
this.test(1).y = s(1:ind(1)-1);
this.test(1).t = ti(1:ind(1)-1);
this.test(end+1).y = s(ind(end)+1:end);
this.test(end).t = ti(ind(end)+1:end);
elseif length(ind) >= 2
this.test(1).y = s(1:ind(1)-1);
this.test(1).t = ti(1:ind(1)-1);
for i = 1:length(ind)-1
this.test(i+1).y = s(ind(i)+1:ind(i+1)-1);
this.test(i+1).t = ti(ind(i)+1:ind(i+1)-1);
end
this.test(end+1).y = s(ind(end)+1:end);
this.test(end).t = ti(ind(end)+1:end);
end
y = this.test;
end
Let's say x,t will be chopped into 3 parts, so now segment will have 3 vales.
segment(1).y,segment(1).t
segment(2).y,segment(2).t
segment(3).y,segment(3).t
I want to implement a function in a data handle class, where it remembers the old data and adds the new one. for example:
segment = f.addsig(signal,time)
segment = f.addsig(signal2,time2)
so we know that x1,t1 will be chopped into 3 parts. and say x2,t2 should be chopped into 2 parts., so now segment should have 5 parts.
segment(1).y,segment(1).t
segment(2).y,segment(2).t
segment(3).y,segment(3).t
segment(4).y,segment(4).t
segment(5).y,segment(5).t
I need to do this in datahandle class, not in my testscript. any suggestions will be helpful
I can call the addsign function N number of times, and each signal can be chopped into N number of segments, i want a logic which handles arbitrary number of signals
2 Comments
Guillaume
on 11 Jul 2016
Your post and your code would be much easier to understand if you'd used variable names that have meaning. It also helps self document the code since there's no comments.
As it is, I keep having to scroll back to find out what each variable is. What's x? Ah yes, it's the signal. So why not call it signal? What's a again? The diff of the time signal (maybe?), so let's call that timeinterval, etc.
In the end, I've scrolled back and forth trying to understand what is what but still have no idea of your precise question. You've posted code for a chopp method but are asking about an addsig method. How are the two related?
JA
on 11 Jul 2016
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files 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!