check if a cell contains the number 2 in matlab

You can check if a cell contains the number 2 in MATLAB by converting the cell to a string array and then using the contains function. Here is an example:

main.m
% define cell array
C = {'apple', 5, 2, 'banana'};

% convert to string array
S = string(C);

% check if array contains '2'
contains(S, '2')
141 chars
9 lines

This will return a logical array where 1 means that the corresponding element contains the number 2 and 0 means that it does not.

related categories

gistlibby LogSnag