# How do I cut the columns in a cell of a cell array according to values in a numerical array?

5 views (last 30 days)
lil brain on 16 Dec 2022
Commented: John on 19 Dec 2022
Hi,
I have the following problem at hand:
I have a cell array called alldata with 24 cells. The first column in each cell of alldata contains seconds as single numbers. The second column contains measurment points.
I am trying to loop through all cells in alldata and shorten the columns in each cell at the beginning and at the end a little (by deleting the first few and the last few rows) (but not for every column).
To do this, I also have a numerical array called beginning_end with 24 rows relating to each of the 24 cells in alldata. The first column in beginning_end contains cut-off points (seconds in numbers) where I want to delete the first few seconds of a column in a cell and the second column in beginning_end contains cut-off points where I want to delete the last few seconds of a column in a cell.
If there is a NaN then I dont need to cut anything (from the beginning or end of a column).
To do this I would need to (find and) match the seconds in the rows of beginning_end with the seconds in the rows of the cells of alldata to get the row numbers and know where to cut correct?
How would such code look?
(I hope this is coherent enough to understand)
Thanks!

John on 16 Dec 2022
Edited: John on 16 Dec 2022
I've made this a little long in order to improve legibility. Hope this works!
% Make sample data
foo = zeros(100, 2);
bar = zeros(100, 2);
for i = 2:100
foo(i, 1) = foo(i-1) + rand;
bar(i, 1) = bar(i-1) + rand;
foo(i, 2) = i;
bar(i, 2) = i;
end
alldata = {foo, bar};
beginning_end = [5.2, 20.1; 2.0, 25.5];
% Iterate through and filter
for i = 1:length(alldata)
above_min = alldata{i}(:, 1) > beginning_end(i, 1);
below_max = alldata{i}(:, 1) < beginning_end(i, 2);
valid_rows = above_min & below_max;
to_keep = alldata{i}([valid_rows, valid_rows]);
to_keep = reshape(to_keep, [height(to_keep)/2, 2]); %Reshape from logical output
alldata{i} = to_keep;
end
% Check that the data was filtered (this is just for error checking)
for i = 1:length(alldata)
disp(sprintf('Data array %d, Height=%d', i, height(alldata{i})))
disp(alldata{i}(1:2, :))
disp(sprintf('\t ...'))
disp(alldata{i}(end-1:end, :))
end
Data array 1, Height=28
5.4575 9.0000 5.9736 10.0000
...
19.1717 35.0000 19.8663 36.0000
Data array 2, Height=48
2.5396 6.0000 3.2197 7.0000
...
25.0505 52.0000 25.2473 53.0000
John on 19 Dec 2022
Great, hope you were able to get it to work!