Extracting information from multiple tables within a cell to plot

조회 수: 10(최근 30일)
Nicholas Scott
Nicholas Scott 2022년 3월 8일
댓글: Peter Perkins 2022년 3월 10일
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)
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,

채택된 답변

Voss 2022년 3월 9일
편집: Voss 2022년 3월 9일
% load data and plot:
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
% 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));
% plot
  댓글 수: 2

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Peter Perkins
Peter Perkins 2022년 3월 9일
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");
hold off
  댓글 수: 2
Peter Perkins
Peter Perkins 2022년 3월 10일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by