Clear Filters
Clear Filters

How to separate a cell array based on existing strings?

3 views (last 30 days)
I have a cell array that is 2452x5 but it has been reduced for the purpose of this question. I would like to separate the cell array, raw, into separate matrices based on a string within rows ('VR26'). The string isn't always present in the first column, the cell array below was simplified.
raw =
'VR26' [ 0.0539]
'VR26' [ 0.0015]
'VR26' [ 14.0853]
'VR27' [ 0.0658]
'VR27' [ 0.0022]
'VR27' [ 25.7290]
'VR40' [ 0.1425]
'VR40' [ 0.0566]
'VR40' [ 1.7189]
'VR40' [ 1.3351]
'VR40' [ 6.0185e-04]
'VR40' [ 0.0085]
'VR40' [ 8.6243]
Be split into...
VR26 =
'VR26' [ 0.0539]
'VR26' [ 0.0015]
'VR26' [ 14.0853]
raw2 =
'VR27' [ 0.0658]
'VR27' [ 0.0022]
'VR27' [ 25.7290]
'VR40' [ 0.1425]
'VR40' [ 0.0566]
'VR40' [ 1.7189]
'VR40' [ 1.3351]
'VR40' [6.0185e-04]
'VR40' [ 0.0085]
'VR40' [ 8.6243]

Accepted Answer

Akira Agata
Akira Agata on 15 Jul 2017
You can do this by using cellfun function, like:
% Sample cell array
C = [{'VR26';'VR26';'VR27';'VR40'},num2cell(rand(4,1))];
idx = cellfun(@(x) strcmp(x, 'VR26'), C(:,1));
% Extracted data (VR26)
C1 = C(idx,:);
% Others
C2 = C(~idx,:);
  1 Comment
khadija portu
khadija portu on 10 Jul 2019
thank you for this code
i want to separate a matrix with binary code with 10 rows into 2 matrix five each, what should i add bcz after running that i had like each column itsef could you tell me what should i do

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!