You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Extracting data range from array problem
4 views (last 30 days)
Show older comments
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
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
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.
Accepted Answer
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
on 13 May 2022
That looks like its returning exactly what I need.
Now I need to spend some time trying to understand it!!
Really appreciate the help.
Nigel Davis
on 14 May 2022
I can see your code reshapes the array to a calculated size by twice the samTurns number + 1. Could you tell my why the below line in my initial code doesnt work:
t1acdc=ipArray(peakIdx-sampTurns:peakIdx+sampTurns,2)
It returns a set of values for the first index but not the rest?
Also while palying around with the code you provided, I found it works really well up to a sampTurns value of 79, any higher than that and I get an error. What is the reason for that and is there a fix for it thats not really complicated?!!
Thanks.
Voss
on 14 May 2022
Edited: Voss
on 14 May 2022
The reason that this line
t1acdc=ipArray(peakIdx-sampTurns:peakIdx+sampTurns,2)
only gives you the points around the first peak is because it's using a vector as an argument to the colon operator (peakIdx is a vector, so peakIdx-sampTurns and peakIdx+sampTurns are both vectors). See what happens when you do that:
[21 45 69]:[31 55 79] % example 1
ans = 1×11
21 22 23 24 25 26 27 28 29 30 31
[21 61 91 301]:[31 65 99 302] % example 2
ans = 1×11
21 22 23 24 25 26 27 28 29 30 31
The colon operator only uses the first element of each vector. This makes sense, in a way, because how should the colon operator handle building multiple arrays that are different lengths, like in example 2 there? Maybe it could put them in a cell array and return {21:31 61:65 91:99 301:302}, I suppose, but it does not.
So that's why in my answer I make a matrix of indices
[21; 45; 69]+(-5:5)
ans = 3×11
16 17 18 19 20 21 22 23 24 25 26
40 41 42 43 44 45 46 47 48 49 50
64 65 66 67 68 69 70 71 72 73 74
and use those as the row index into ipArray with column index 2, and reshape the result.
Now, for the second question, why do you get an error with sampTurns = 80 or higher? It looks like the first peak returned from findpeaks is at index 80, so trying to get elements from the row of ipArray that's 80 rows before that means row 0, which is off the beginning of the array.
sampTurns = 80;
ipArray=csvread('accel test drive 1.csv');
col1 = ipArray(:, 1);
[peaks,peakIdx] = findpeaks(col1,'MinPeakDistance',300, "MinPeakHeight",10)
peaks = 7×1
12.5200
16.4400
32.7900
26.6900
24.2500
28.5700
26.9000
peakIdx = 7×1
80
501
939
1810
2567
2899
3362
peakIdx+(-sampTurns:sampTurns)
ans = 7×161
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
(Notice the first element in that matrix is 0, which is not a valid index to use.)
With different data or different input parameters in findpeaks, it could happen that the code would try to access a row off the end of ipArray, i.e., a row whose index is greater than size(ipArray,1).
One way to handle these cases and prevent those types of error would be:
- to store those row indices before they're used
- somehow keep track of (or "mark") which row indices are invalid
- replace the invalid values with some valid index
- do the indexing into the rows of ipArray with the "corrected" values
- replace the values in the result t1acdc that correspond to invalid indices with NaN, using the "mark" found before
That would look like this:
% store the row indices:
rowIdx = peakIdx+(-sampTurns:sampTurns);
% create a marker/indicator - a logical matrix saying
% whether each element of rowIdx is invalid:
isBadIdx = rowIdx < 1 | rowIdx > size(ipArray,1);
% replace the invalid row indices with 1
rowIdx(isBadIdx) = 1;
% do the indexing, get the result
t1acdc = reshape(ipArray(rowIdx,2),[],2*sampTurns+1);
% put NaNs in the result where it would've come
% from an invalid row index
t1acdc(isBadIdx) = NaN;
And here's the same thing again, but showing the output of each step. In this case, the only invalid index is the first one (but increasing sampTurns beyond 80 would give you more invalid indices and more corresponding NaNs in the result):
rowIdx = peakIdx+(-sampTurns:sampTurns)
rowIdx = 7×161
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
isBadIdx = rowIdx < 1 | rowIdx > size(ipArray,1)
isBadIdx = 7×161 logical array
1 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rowIdx(isBadIdx) = 1
rowIdx = 7×161
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
t1acdc = reshape(ipArray(rowIdx,2),[],2*sampTurns+1)
t1acdc = 7×161
0.1600 0.1600 0.5600 0.6500 0.6000 0.7000 0.5700 0.4700 0.7500 1.5800 1.4200 0.3300 -0.9800 -2.3000 -3.1500 -3.3300 -3.2400 -2.9600 -2.7700 -2.6000 -2.3600 -2.0000 -1.5100 -1.0900 -0.7400 -0.4600 -0.1600 0.1200 0.2500 0.5100
31.2200 30.0500 28.8100 27.4400 25.9300 24.4700 22.9700 21.4600 20.0300 18.6300 17.2800 15.8300 14.2700 12.7100 11.2900 9.7500 8.4900 7.2400 6.1900 5.3100 4.5900 4.0000 3.4100 2.8200 2.3200 1.9000 1.5100 1.2100 0.9400 0.7000
40.3500 40.2300 40.1100 40.0500 39.9600 39.8800 39.7500 39.6600 39.7400 39.7400 39.7800 39.7700 39.7500 39.5600 39.4700 39.4300 39.2000 38.9700 38.5500 38.0800 37.4500 36.8500 36.2700 35.6700 35.0200 34.2200 33.2900 32.3500 31.2900 30.1000
20.7400 20.7700 20.7500 20.7000 20.7600 20.8400 20.8000 20.8000 20.7800 20.6800 20.5200 20.2800 20.0100 19.6600 19.2100 18.7400 18.2200 17.6500 17.0300 16.4400 15.8100 15.1600 14.5300 14.0500 13.5800 13.0700 12.7400 12.5100 12.3100 12.1200
23.9900 23.8600 23.7700 23.7400 23.8200 24.0800 24.2000 24.1400 23.9900 23.7200 23.5000 23.3600 23.3800 23.4400 23.5200 23.7400 23.9300 24.0200 24.0600 24.0300 23.9900 24.0300 24.2000 24.5000 24.7600 24.9700 25.1600 25.2800 25.5100 25.7100
31.2200 31.8700 32.5600 33.0700 33.4900 33.8300 34.0600 34.1200 34.0500 33.9700 33.8600 33.8500 33.8700 33.7100 33.4700 33.2700 32.9500 32.5000 32.1400 31.8500 31.4500 31.1600 31.0400 31.0900 31.3100 31.4400 31.3000 30.9200 30.4400 29.9600
-28.3900 -28.4200 -28.5800 -28.8200 -29.0800 -29.2900 -29.4100 -29.5300 -29.6100 -29.6600 -29.7500 -29.8700 -30.1000 -30.3600 -30.6400 -30.9000 -31.0400 -31.1600 -31.3600 -31.5300 -31.6500 -31.7500 -31.7500 -31.7100 -31.6000 -31.4400 -31.3200 -31.2600 -31.2600 -31.4000
t1acdc(isBadIdx) = NaN
t1acdc = 7×161
NaN 0.1600 0.5600 0.6500 0.6000 0.7000 0.5700 0.4700 0.7500 1.5800 1.4200 0.3300 -0.9800 -2.3000 -3.1500 -3.3300 -3.2400 -2.9600 -2.7700 -2.6000 -2.3600 -2.0000 -1.5100 -1.0900 -0.7400 -0.4600 -0.1600 0.1200 0.2500 0.5100
31.2200 30.0500 28.8100 27.4400 25.9300 24.4700 22.9700 21.4600 20.0300 18.6300 17.2800 15.8300 14.2700 12.7100 11.2900 9.7500 8.4900 7.2400 6.1900 5.3100 4.5900 4.0000 3.4100 2.8200 2.3200 1.9000 1.5100 1.2100 0.9400 0.7000
40.3500 40.2300 40.1100 40.0500 39.9600 39.8800 39.7500 39.6600 39.7400 39.7400 39.7800 39.7700 39.7500 39.5600 39.4700 39.4300 39.2000 38.9700 38.5500 38.0800 37.4500 36.8500 36.2700 35.6700 35.0200 34.2200 33.2900 32.3500 31.2900 30.1000
20.7400 20.7700 20.7500 20.7000 20.7600 20.8400 20.8000 20.8000 20.7800 20.6800 20.5200 20.2800 20.0100 19.6600 19.2100 18.7400 18.2200 17.6500 17.0300 16.4400 15.8100 15.1600 14.5300 14.0500 13.5800 13.0700 12.7400 12.5100 12.3100 12.1200
23.9900 23.8600 23.7700 23.7400 23.8200 24.0800 24.2000 24.1400 23.9900 23.7200 23.5000 23.3600 23.3800 23.4400 23.5200 23.7400 23.9300 24.0200 24.0600 24.0300 23.9900 24.0300 24.2000 24.5000 24.7600 24.9700 25.1600 25.2800 25.5100 25.7100
31.2200 31.8700 32.5600 33.0700 33.4900 33.8300 34.0600 34.1200 34.0500 33.9700 33.8600 33.8500 33.8700 33.7100 33.4700 33.2700 32.9500 32.5000 32.1400 31.8500 31.4500 31.1600 31.0400 31.0900 31.3100 31.4400 31.3000 30.9200 30.4400 29.9600
-28.3900 -28.4200 -28.5800 -28.8200 -29.0800 -29.2900 -29.4100 -29.5300 -29.6100 -29.6600 -29.7500 -29.8700 -30.1000 -30.3600 -30.6400 -30.9000 -31.0400 -31.1600 -31.3600 -31.5300 -31.6500 -31.7500 -31.7500 -31.7100 -31.6000 -31.4400 -31.3200 -31.2600 -31.2600 -31.4000
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!
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)