Error when trying to remove rows with specific values from cell array.

I have a cell array that looks like this with strings and numbers
I'm trying to write simple code that will remove entire rows in the array if numbers 1-20 are present in column 1:
toremove = [1:20];
event_time(ismember(event_time(:,1),toremove)) = [];
However I receive this error when I run my code
Error using cell/ismember
Input A of class cell and input B of class double must be cell arrays of character vectors, unless one is a character vector.
How would I index event_time by the numbers in column 1? They are not strings/characters so strcmp does not work, and treating them as integers, as above does not work either. Many thanks!

 Accepted Answer

Try this:
CA = num2cell(randi(50, 25, 5)); % Create Cell Array
idx = cellfun(@(x)ismember(x, 1:20), CA(:,1)); % Logical Vector Selecting (1:20) In Column #1
CAedited = CA(~idx,:); % Eliminat Rows Meeting Criteria
Use your own cell array for ‘CA’.

6 Comments

Hi, thank you. The idx part worked when I set 'Uniform Output' to false as so:
idx = cellfun(@(x)ismember(x, 1:20), event_time(:,1), 'UniformOutput', false);
As the first row begins with 'Trial'. But when I tried to use the index to remove certain rows I received the following error:
Undefined unary operator '~' for input arguments of type 'cell'.
I suppose the issue stems from me having both strings and numbers in the array?
My code should work with your cell array. It worked with the cell array I created to test my code.
I do not have your cell array (or a representative sample of it) to work with. It would be easier to solve this problem if I did.
Yes I tried it on your cell array and indeed it worked fine. I've attached my cell array to the question, let me know if you're able to access it, thank you!
There was a minor problem with respect to the class of the ‘idx’ vector that was not a problem with my cell array I used to test my code.
This appears to solve that problem:
C1 = load('blk1_event_times.mat'); % Read .mat File
event_time = C1.event_time; % Cell Array
idx = cellfun(@(x)ismember(x, 1:20), event_time(2:end,1), 'Uni',0); % Logical Vector Selecting (1:20) In Column #1
event_time_edited = event_time(~[idx{:}],:); % Eliminate Rows Meeting Criteria
The ‘idx’ logical vector cell array needs to be converted to a ‘regular’ (for lack of a better adjective) logical vector for this to work. One approach would be to use the cell2mat function, although the ‘shorthand’ ‘[idx{:}]’ works well here.
I did not look closely at the output ‘event_time_edited’, however the original cell array is (314x8) and ‘event_time_edited’ is (273x8), so I assume it works correctly.
Many thanks! I tweaked your code a bit so that idx takes into account the first row in event_time which contains 'trial'. Without taking this into account, the idx file is too short (with one row less than the number of rows of the original file)
idx = cellfun(@(x)ismember(x, 1:20), event_time(1:end,1), 'Uni',0); % Idx outputs 1 if 1-20 is in Column #1 and 0 otherwise
idx(1,:)= {[0]}; % need to convert first row of index to 0 because the string 'trial' shows [0,0,0,0,0], which will make the index fail to work
event_time = event_time(~[idx{:}],:); % remove any rows where idx = 1

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!