Extracting information from multiple tables within a cell to plot

8 views (last 30 days)
Nicholas Scott
Nicholas Scott on 8 Mar 2022
Commented: Peter Perkins on 10 Mar 2022
I have a cell with multiple tables and I am trying to extract information from to plot the median line of all the separate lines.
Currently, I plot all lines in grey.
for i= 1:length(ArrayPR) %i=Nucleus number
if height(ArrayPR{i}) >= 0.75*max(table_heightsPR)
plot(ArrayPR{i,1}.ImageNumber,ArrayPR{i,1}.Intensity_MeanIntensity_Alexa488,'color',[0,0,0]+0.75)
else
end
end
I also would like to plot the median line out of all of those as a thicker line like so:
plot(ArrayPR{:,1}.ImageNumber,median(ArrayPR{i,1}.Intensity_MedianIntensity_Alexa488),'k', 'LineWidth', 2)
However, I am not telling it to use the correct value to median, and I can't wrap my head around how to do so.
What i need it to do, is go into every table and look at the same image number (column one in ArrayPR{i,1}). I then need it to find the median of that one image number across all 'i', and then proceed to the next image number. Then plot that final line. I'm basically making an average line from all the different lines plotted, but I want the line to be less susceptible to outliers, which these data sets frequently see.
It is also possible that some of the elements within the ArrayPR don't have the maximum table height (some are even ignored with the if statement I have created), but when looking through all of the different rows of the ArrayPR, if it doesn't find a specific image number in a given 'i' it needs to ignore that specific 'i' for that given image number.
How does one accomplish this? Thanks,
Nick

Accepted Answer

Voss
Voss on 9 Mar 2022
Edited: Voss on 9 Mar 2022
% load data and plot:
load('matlab.mat');
figure();
hold on
table_heightsPR = cellfun(@height,ArrayPR);
height_threshold = 0.75*max(table_heightsPR);
for i = 1:numel(ArrayPR)
if table_heightsPR(i) >= height_threshold
plot(ArrayPR{i,1}.ImageNumber,ArrayPR{i,1}.Intensity_MeanIntensity_Alexa488,'color',[0,0,0]+0.75);
else
end
end
% median intensity for each image number calculation:
% first, concatenate all the tables into one big table:
% t = vertcat(ArrayPR{:});
% or concatenate just the tables whose height is >= height_threshold:
t = vertcat(ArrayPR{table_heightsPR >= height_threshold});
% get all the image numbers:
all_image_number = t.ImageNumber;
% get all the intensities:
all_intensity = t.Intensity_MeanIntensity_Alexa488;
% get the unique image numbers:
u_image_number = unique(all_image_number);
% calculate the median intensity for each image number:
% number of unique image numbers:
n_images = numel(u_image_number);
% pre-allocating the median intensity array:
median_intensity = zeros(1,n_images);
% for each unique image number:
for i = 1:n_images
% get a logical index saying whether each element of all_image_number
% is equal to this image number (idx is true where
% all_image_number == u_image_number(i) and false elsewhere):
idx = all_image_number == u_image_number(i);
% use that logical index to calculate the median intensity for this
% image number:
median_intensity(i) = median(all_intensity(idx));
end
% plot
plot(u_image_number,median_intensity,'k','LineWidth',2);
  2 Comments

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 9 Mar 2022
Here's a version that uses grouped rowfun and varfun to do the heavy lifting:
load matlab.mat
table_heightsPR = cellfun(@height,ArrayPR);
height_threshold = 0.75*max(table_heightsPR);
keep = cellfun(@(t) height(t)>=height_threshold, ArrayPR);
t = vertcat(ArrayPR{keep});
t = t(:,["ImageNumber" "ObjectNumber" "Intensity_MeanIntensity_Alexa488"]);
figure; hold on
rowfun(@(inum,int)plot(inum,int,'color',[.75 .75 .75]), t, "GroupingVariables","ObjectNumber","NumOutputs",0);
med = varfun(@median, t,"InputVariables","Intensity_MeanIntensity_Alexa488","GroupingVariables","ImageNumber");
plot(med.ImageNumber,med.median_Intensity_MeanIntensity_Alexa488,'k-','LineWidth',2);
hold off
  2 Comments
Peter Perkins
Peter Perkins on 10 Mar 2022
A grouped varfun applies a function to groups of rows in each variable in the table, separately. In this case, the function is median.
A grouped rowfun applies a function to groups of rows in a table, where that function accepts multiple variables. In this case, the function is plot, with some extra arguments (thus the anonymous function).
There are ungrouped versions of both.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!