How to extract data to a matrix from cell arrays with varying cell lengths?
6 views (last 30 days)
Show older comments
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!
Answers (1)
madhan ravi
on 14 Aug 2019
xy = cat(1,cell_array{:});
x = xy(:,1)
y = xy(:,2)
3 Comments
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!