MATLAB Answers

How to extract data to a matrix from cell arrays with varying cell lengths?

2 views (last 30 days)
Stephen Zambrzycki
Stephen Zambrzycki on 14 Aug 2019
Hello, I'm using a peak picking command "mspeaks" and after it process my data, it outputs it into a cell array with the following format (92x1 cell). Each row corresponding to a different sample.
76x2 double
74x2 double
72x2 double
30x2 double
43x2 double
32x2 double
26x2 double
65x2 double
etc...
In each individual cell, the first column is the x-axis point (m/z) and the second column is the corresponding y-value (intensity). I tried using the following loop, but the different number of rows/peaks detected causes an error.
% PP_ERMNBP = 'the cell array mentioned above'
%Extract the peak list values
for i=1:size(PP_ERMNBP,1)
PP_extract_mz(:,i)=PP_ERMNBP{i,1}(:,1)
PP_extract_Y(:,i)=PP_ERMNBP{i,1}(:,2)
end
%%Resulting Error
%%Unable to perform assignment because the size of the left side is 76-by-1 and the size of the right side is 74-by-1.
Using "cell2mat" directly converts all the data into a single two column matrix which isn't useful, and transposing the cell array before hand also causes the same error.
I would like to extract all the x-axis points into one array/matrix, and all the y-axis points into another becauses I want to do some other operations and eventually save the data to a single .csv table (which I already have the script for).
Thanks for your help!

  1 Comment

Stephen Zambrzycki
Stephen Zambrzycki on 14 Aug 2019
I forgot to explain that the in the "x-axis points into one array/matrix, and all the y-axis points into another " section, that each column in the array should correspond to individual sample.
i.e.
[sample1 sample2 sample3 etc...]
%or
['76x1 double' '74x1 double' '72x1 double' etc...]

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 14 Aug 2019
xy = cat(1,cell_array{:});
x = xy(:,1)
y = xy(:,2)

  3 Comments

Stephen Zambrzycki
Stephen Zambrzycki on 14 Aug 2019
Unfortunately, this is the same problem I'm getting with 'cell2mat', but seperates the columns into two different variables.
I forgot to explain that the in the "x-axis points into one array/matrix, and all the y-axis points into another " section, that each column in the array should each individual sample.
i.e.
[sample1 sample2 sample3 etc...]
madhan ravi
madhan ravi on 14 Aug 2019
I have completely no idea what your trying to say with your comment. But the above solution should work flawlessly as shown for your data in the question above.
Stephen Zambrzycki
Stephen Zambrzycki on 14 Aug 2019
I apologize for the confusion and appreciate the time you took to answer the question, but it's not exactly what I'm looking for.
I used your code, and it gives me two matrices. One with all the x-values compiled into one column and other array with all the y-values in one column. With this, I can't distinguish between different samples because they are all in a single column.
I was hoping for a matrix where each column is a sample. For example in my data set, the matrix would contain 92 columns accounting for the 92 samples/cells that I have. Each row would be a peak that was detected.

Sign in to comment.

Sign in to answer this question.