splitting periodic data using cellarray

Hi,
I have a set of data which is periodic and in each period there is an ascending behaviour. I would like to split this data into sets of only ascending data using a cell array. My code has been successful in creating the first set in the first row of the cell array. My question is: how can I continue with the loop and write the data on the second row of the cell array?
Thanks for your help in advance :)
Here is my code:
auxArray={};
for j=2:1:size(newV_ch)
if newV_ch(j)-newV_ch(j-1)>0
auxArray{end+1}=newV(j);
else
break
end
end

 Accepted Answer

The actual result depends on the signal (that may need to be filtered if it is noisy), however the code would be something like this —
t = linspace(0, 50, 500); % Time Vector
y = sin(2*pi*t/10); % Signal Vector
[pks,locsp] = findpeaks(y); % Find Maxima & Indices
[vls,locsv] = findpeaks(-y); % Find Minima & Indices
pkidx = sort([locsv locsp(locsp > locsv(1))]); % Edit & Sort Indices
for k = 1:2:numel(pkidx)-1
idxrng = pkidx(k):pkidx(k+1); % Index Range For Each Segment
tv{k} = t(idxrng); % Corrseponding Time Vector
Asc{k} = y(idxrng); % Corresponding Signal Segment Vector
end
figure
plot(t, y) % Plot Original Signal
hold on
for k = 1:numel(tv)
plot(tv{k}, Asc{k}, '-r', 'LineWidth',2) % plot Results
end
hold off
grid
It should be relatively straightforward for you to adapt this to your signal.

6 Comments

Thanks alot for your reply :)
This is brilliant, thank you very much!
Running the code for my data, I faced an error in the sort function saying that 'Dimensions of arrays being concatenated are not consistent.' Could you please help me with this problem as well?
My pleasure!
I would need your data to figure out exactly what the problem is, especially since the error message or the line that threw the error were not provided.
However since that may be the ‘pkidx’ assignment, a fix for that problem would be (for all such vectors) —
locsp = locsp(:); % Force Column Vector
locsv = locsv(:); % Force Column Vector
pkidx = sort([locsv; locsp(locsp > locsv(1))]); % Edit & Sort Indices
This forces the vectors to be column vectors, then concatenates and sorts them. This (and the rest of the code) will then work whether the original data were row or column vectors.
.
Thanks for your reply :)
I tried the code, it solved the error but the results are not correct. I tried to manipulate the code to adapt to my data but I was not successful in doing so. Attached you can find my data. I really appreciate it if you can take a look and let me know how I can adapt this code to my data.
Thanks for your help in advance!
My pleasure!
This is the problem with not having the data at the outset.
A few tweaks were needed, because the beginning of the first uplope began at the beginning of the vector, and the last upslope ended at the end of the vector, so including them, ‘pkidx’ becomes:
pkidx = sort([locsv; locsp; 1; L]); % Edit & Sort Indices
with the slightly edited full code now being:
LD = load('matlab.mat');
y = LD.newV_ch;
L = numel(y);
t = linspace(0, L-1, L);
[pks,locsp] = findpeaks(y); % Find Maxima & Indices
[vls,locsv] = findpeaks(-y); % Find Minima & Indices
locsp = locsp(:); % Force Column Vector
locsv = locsv(:); % Force Column Vector
pkidx = sort([locsv; locsp; 1; L]); % Edit & Sort Indices
for k = 1:2:numel(pkidx)-1
idxrng = pkidx(k):pkidx(k+1); % Index Range For Each Segment
tv{k} = t(idxrng); % Corrseponding Time Vector
Asc{k} = y(idxrng); % Corresponding Signal Segment Vector
end
figure
plot(t, y) % Plot Original Signal
hold on
for k = 1:numel(tv)
plot(tv{k}, Asc{k}, '-r', 'LineWidth',2) % plot Results
end
hold off
grid
producing this result:
The only significant change was in to add the ‘t’ vector (necessary for the plot to work), and tweak ‘pkidx’, with the rest of the code unchanged. If other records so not have upslopes beginning at the start of the vector and and ending with the end of the vector, the original ‘pkidx’ will likely work best. Tweak it appropriately to work with different data vectors. It would be difficult to code a ‘universal’ analysis aproach without knowing more about the data and the desired result.
.
Thank you very much! It was a big help :)
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!