How do i retrieve data from a vector of idexes?

2 views (last 30 days)
Reachnext is a vector that contains the indexes of certain files i want. These files are within the larger file MCR_full.MIB037.Reaches. How do i retrieve the actual files within reachnext and not just their index?
I tried writing the following
temp = (MCR_full.MIB037.Reaches.(reachnext(i,k).kin));
but got this error
Index in position 1 exceeds array bounds. Index must not exceed 1.
any help would be great!
thank you!
  1 Comment
James Tursa
James Tursa on 26 Jan 2022
You need to give us more information. What exactly are the data types and sizes of all of the variables, including the fields, that you are working with?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 26 Jan 2022
Reachnext is a vector that contains the indexes of certain files i want
Then Reachnext contains numeric values? But your code uses reachnext(i,k).kin which would require that reachnext(i,k) evaluates to either a struct or an object
Your code would be consistent with the possibility that reachnext is a struct array and that reachnext(i,j) is a scalar struct that has a field named kin that contains a character vector, and that the contents of that character vector identify a field to use within the scalar struct MCR_full.MIB037.Reaches .
If that were the case then the error would just be that the nonscalar struct reachnext only has one row
  2 Comments
Mackenzie Maher
Mackenzie Maher on 26 Jan 2022
Edited: Walter Roberson on 27 Jan 2022
I believe you're right but i dont know how to fix it.
Here's the rest of the code.
fn = fieldnames(MCR_full.MIB037.Reaches); %get fieldnames
allPeaks = cell(10,1);
prominencePeaks = zeros([],[]);
for k = 1:10 %loop through first ten reaches
kins = MCR_full.MIB037.Reaches.(fn{k}).kin; %get kinematics array
t = kins(:, 1); %first column is time
y = kins(:, 3); %third column is 'up'. switch to '2' for 'out'.
%Use findpeaks to get all peak values ('peaks') and locations
%('peaks_inx'). Only get peaks above prominence 0.1.
peaks = findpeaks(y);
reachLimitArray = find(peaks >= 0.1);
allPeaks{k} = reachLimitArray;
%Plotting
if length(reachLimitArray) > 1
disp ('There is at least two value above the limit.')
f=figure(k);
hold on;
plot(t, y, 'b-', 'LineWidth', 2);
plot(t(peaks), peaks, 'rv', 'LineWidth', 2, 'MarkerSize', 12);
grid on;
caption = sprintf('Peaks for %s', (fn{k}));
title(caption, 'FontSize', 18);
for i = 1:length(reachlimit)
index = reachLimit(i);
prominencePeaks(k,i) = peaks(index);
end
end
end
peakID = any(prominencePeaks,2);
doublereaches=find(peakID);
reachnext=find(peakID)+1;
Walter Roberson
Walter Roberson on 27 Jan 2022
t = kins(:, 1); %first column is time
y = kins(:, 3); %third column is 'up'. switch to '2' for 'out'.
Is kins a string() array that contains "up" or "2" or "out" in the third column, and a string-represented time in the first column? I am concerned because in context, your y would have to be a cell array of character vectors, or a categorical array, or a string array, in order to be able to hold the 'up' representation. Of those, categorical is the only one that you might hope to be able to findpeaks() on.
for i = 1:length(reachlimit)
index = reachLimit(i);
prominencePeaks(k,i) = peaks(index);
end
You could vectorize part of that:
high_peaks = peaks(reachlimit);
prominencePeaks(k,1:length(high_peaks)) = high_peaks;

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!