How to extract particular row values from cell arrays with varying cell lengths?

3 views (last 30 days)
Dear Matlab users, I am new to Matlab and especially handling cell arrays. I need assistance! I hope I find a solution.
I have a struct array, in that there are 2 cell arrays (Signal and Label), each of size 1*117 cell and each cell has different dimensions. So, label are from numbered from 1 to 4, and they are always in a sequence like 4,4,4,1,1,1,2,2,2,3,3,4,4,4,4,1.... so on. When label starts with 1 and ends 4 it is taken as 1 fragment/segment. It can have any number of 1,2,3,4 in them.So I want to extract first fragment from each cell (all 117) such that it comes with signal and label in one struct or 2 different cell arrays so that I can use it for training and further classification.
How would you extract it? Thank you in advance :)
here are images for the reference: total_states are labels numbered from 1 to 4, total_state_test_recordings are signal values for each label.
Also, note. dimensions for labels and signal for each cell is same.
1*117 cell of label values (total_states)
first cell of label
first cell of signal
  9 Comments
Bhakti Khera
Bhakti Khera on 7 Sep 2022
I sincerely appreciate the time and effort you put into this. I've included a small dataset in the previous comment that could serve as a sample of the bigger dataset; is this what you were looking for?

Sign in to comment.

Accepted Answer

Voss
Voss on 6 Sep 2022
N = numel(x.total_states);
first_fragment_labels = cell(1,N);
first_fragment_signal = cell(1,N);
for ii = 1:N
idx1 = find(x.total_states{ii} == 1, 1);
if isempty(idx1)
continue
end
idx2 = find(diff(x.total_states{ii}(idx1:end)) == -3, 1) + idx1 - 1;
if isempty(idx2)
continue
end
first_fragment_labels{ii} = x.total_states{ii}(idx1:idx2);
first_fragment_signal{ii} = x.total_test_recording{ii}(idx1:idx2);
end

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by