I figured it out:
datestring = zeros(length(cell_array), 5);
distribution(:, :, 1) = strcmp(cell_array, 'year');
distribution(:, :, 2) = strcmp(cell_array, 'day');
distribution(:, :, 3) = strcmp(cell_array, 'hour');
distribution(:, :, 4) = strcmp(cell_array, 'minute');
distribution(:, :, 5) = strcmp(cell_array, 'sec');
distribution = circshift(distribution, [0 1]);
distribution_perm = permute(distribution,[2 1 3]);
cell_array_perm = permute(cell_array,[2 1]);
d_str(logical(sum(distribution(:,:,1),2)),1) = cell2mat(cell_array_perm(distribution_perm(:,:,1)))';
d_str(logical(sum(distribution(:,:,2),2)),2) = cell2mat(cell_array_perm(distribution_perm(:,:,2)))';
d_str(logical(sum(distribution(:,:,3),2)),3) = cell2mat(cell_array_perm(distribution_perm(:,:,3)))';
d_str(logical(sum(distribution(:,:,4),2)),4) = cell2mat(cell_array_perm(distribution_perm(:,:,4)))';
d_str(logical(sum(distribution(:,:,5),2)),5) = cell2mat(cell_array_perm(distribution_perm(:,:,5)))';
with provided cell_array(see question). I chose to not use a loop because the input cell array has 30000 cols. The permutation is needed because my code processes each column. If there was no permutation the results would be wrong because e.g. the 'sec' more left in the cell array will come first in the resulting matrix.
3 Comments
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406183
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406183
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406188
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406188
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406270
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/312213-extract-data-from-cell-array-with-specified-key-word-in-other-cell#comment_406270
Sign in to comment.