# How to save the Data in different array using Value in 2nd column

9 views (last 30 days)
Med Future on 24 Jan 2023
Commented: Jan on 24 Jan 2023
Hello, I hope you are doing well. I have the dataset which have five columns
My second columns consists the values 15,37,25 etc
I want to to save data in different array for example if i have value of 15 in 2nd column, i want to save the values of remaining four columns which correspond to 15 in different array.
Similar method for other values too.
How can i do that in MATLAB
Jan on 24 Jan 2023
@Med Future: Please do not address specific users in a question, if they are not involved in the discussion before. Imagine what would happen, if all users try to catch the attraction of some users: They will be overwhelmed by a pile of messages and will disable the notifications to stop the noise.
The most active members of this forum will post an answer if they find some time and know an answer. Pushing them by notifications will reduce their time to read questions only.
Thank you.

Stephen23 on 24 Jan 2023
Edited: Stephen23 on 24 Jan 2023
Clearly creating lots of separate variables in the workspace would be a very bad approach:
So instead we will use the more efficient approach of storing the split data in one cell array:
S = struct with fields:
DatasetValues: [49349×5 double]
M = S.DatasetValues
M = 49349×5
1.0e+06 * 0.2658 0.0000 0.0000 0.0001 0.0000 0.2920 0.0000 0.0000 0.0001 0.0000 0.2932 0.0000 0.0000 0.0001 0.0000 0.3079 0.0000 0.0000 0.0001 0.0000 0.3119 0.0000 0.0000 0.0001 0.0000 0.3147 0.0000 0.0000 0.0001 0.0000 0.3191 0.0000 0.0000 0.0001 0.0000 0.3249 0.0000 0.0000 0.0001 0.0000 0.3577 0.0000 0.0000 0.0001 0.0000 0.3590 0.0000 0.0000 0.0001 0.0000
[U,~,X] = unique(M(:,2));
F = @(n) M(X==n,:);
C = arrayfun(F,U, 'uni',0)
C = 29×1 cell array
{ 2967×5 double } { 6×5 double } { 3122×5 double } { 3×5 double } { 2×5 double } {16076×5 double } {[2.6727e+06 19 6.9581e-04 1.1950 1]} {[6.3468e+05 26 3.0502e-04 1.2750 2]} { 280×5 double } { 170×5 double } {[2.7925e+06 74 2.6536e-04 2.5650 3]} { 1982×5 double } { 4×5 double } { 998×5 double } { 0×5 double } { 0×5 double }
Each cell of C collects the value given in the corresponding location of U.
Although splitting data up is popular with beginners, in general data should be kept together as much as possible. The split-apply-combine workflow might be useful for you, and is supported by a number of functions:

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!