How to separate a cell array based on existing strings?

1 visualizzazione (ultimi 30 giorni)
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]

Risposta accettata

Akira Agata
Akira Agata il 15 Lug 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 Commento
khadija portu
khadija portu il 10 Lug 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by