Extracting data range from array problem

4 views (last 30 days)
Hi
I am not very experienced with Matlab and am having a problem trying extract data from an array.
I need to read a csv with 2 coloumns, I then find the peaks in one coloumn, find their index and return the values of the second coloumn at these indexes.
That part I can do.
Then for each of those values returned, (there will probably be between 5 and 10 values) I want to return the 5 values before and after it.
So ideally I would end up with between 5 and 10 arrays of 11 values each of these arrays would need to be accessed indivdually later.
I think I may be able to use cells but not sure how.
When I run the code I have attached it does exactly what I need, BUT it only does it for first value of peakIdx, not all of them. As a result I end up with 1 array of values (t1acdc) but I need a set of values for each value of peakIdx
Any help you be greatly appriciated.
prompt = "How many ts? ";
numTurns = input(prompt)
prompt = "How many seconds sampled before and after? ";
sampTurns = input(prompt)
ipArray=csvread('accel test drive 1.csv');
col1 = ipArray(:, 1);
[peaks,peakIdx] = findpeaks(col1,'MinPeakDistance',300, "MinPeakHeight",10)
acdcPeaks=ipArray(peakIdx,2)
t1acdc=ipArray(peakIdx-sampTurns:peakIdx+sampTurns,2)
  2 Comments
Ethan Leonard
Ethan Leonard on 13 May 2022
It would help to have the csv. Can you add it to the question? Without knowing otherwise, it's possible that it only returns the first one because the others don't have a separation distance greater than 300, in which case it would be working perfectly.
Nigel Davis
Nigel Davis on 13 May 2022
Hi
Thanks for the quick respone! I just realised I forgot the file, I've added it now.
Not its not the peak detection I'm having a problem with that part works fine.
Its the last line of code I'm having a problem with.

Sign in to comment.

Accepted Answer

Voss
Voss on 13 May 2022
sampTurns = 5;
ipArray=csvread('accel test drive 1.csv');
col1 = ipArray(:, 1);
[peaks,peakIdx] = findpeaks(col1,'MinPeakDistance',300, "MinPeakHeight",10);
acdcPeaks=ipArray(peakIdx,2);
t1acdc = reshape(ipArray(peakIdx+(-sampTurns:sampTurns),2),[],2*sampTurns+1)
t1acdc = 7×11
0.8800 0.7700 0.6700 0.5900 0.6600 0.5200 0.3300 0.3000 0.3400 0.5500 0.9100 -2.2500 -2.4900 -2.7000 -2.8500 -3.0000 -3.1400 -3.2500 -3.2700 -3.2600 -3.2200 -3.1900 1.6300 1.6000 1.4700 1.4400 1.4100 1.3100 1.1500 1.0300 0.9700 0.8600 0.9200 4.9800 5.1900 5.4300 5.8300 6.2000 6.4800 6.6300 6.6400 6.6400 6.7400 6.9000 12.5200 12.3700 12.2400 12.2300 12.3200 12.4700 12.6000 12.6300 12.6600 12.5800 12.3800 3.8600 3.6200 3.4900 3.3400 3.2600 3.2500 3.2200 3.2000 3.2600 3.3800 3.6000 -28.5400 -28.0200 -27.4600 -26.9700 -26.3600 -25.4500 -24.5000 -23.4700 -22.2700 -20.9800 -19.4900
  6 Comments
Nigel Davis
Nigel Davis on 18 May 2022
Wow that was an excellent explanation and very helpful.
I really appriciate the time and effort you put in!
Thanks again!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!