MATLAB Answers

Selectively plotting cell array data

1 view (last 30 days)
Dennis Premoli
Dennis Premoli on 25 Feb 2021
Hi,
I have organised all of my data (stripped from image file names and info) in a cell array (as shown below).
My goal is to generate 1 figure per sample code, each containing a plot for each element distribution available for that sample. (A similar layout to the picture below).
As such, I am looking for a way to selectively plot on the one figure, only data points that are 'tagged' by the same sample code, and start a new figure once it's done with the curent sample.
I have seen something similar done with accumarray, to split up the one larger cell, as such:
>> a = [1,1;1,2.5;1,3;2,2;2,2;2,2;3,10;3,11;3,14;3,19;4,9];
>> C = accumarray(a(:,1),(1:size(a,1))',[],@(r){a(r,:)});
>> C{:}
ans =
1 1
1 2.5
1 3
ans =
2 2
2 2
2 2
ans =
3 10
3 11
3 14
3 19
ans =
4 9
but I am not familiar with accumarray and its syntax, so I am struggling to integrate it within my code.

Answers (1)

Shravan Kumar Vankaramoni
Shravan Kumar Vankaramoni on 26 Mar 2021
Hi Dennis,
Accumarray doesn't work in your case.
B = accumarray(ind,data)
It takes two arguments, indices and data. Based on the indices, it will accumulate the elements in data. And data doesn’t support cell array as input. But you mentioned your data is in cell array. Refer accumarray docs for more info.
Here is one possible solution to group rows of same sample code if you have data in cell array.
A = {'150c',15,20;'150c',16,21;'151a',10,11;'151a',11,12};
% The following statements gets us the number of unique labels in sample
% code column
B = cell2mat(A(:,1));
C = unique(string(B));
%Converting cell array to table that makes us easy to group rows with same sample code
table = cell2table(A);
% loop over all unique sample codes
for i = 1:length(C)
a = table.A1 == C(i);
%out contains the rows with one sample code
out = table(a,:);
%you can use plot statements here
end

Community Treasure Hunt

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

Start Hunting!