Extracting information from multiple tables within a cell to plot

29 ビュー (過去 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)
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

採用された回答

Voss
Voss 2022 年 3 月 9 日
編集済み: Voss 2022 年 3 月 9 日
% 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 件のコメント
Nicholas Scott
Nicholas Scott 2022 年 3 月 9 日
That is impressive, while this is probably very basic for you, I cannot wait for me to be able to think of code to do this on the fly!
You are helping me learn a lot! Thank you@_!
Voss
Voss 2022 年 3 月 9 日
You're welcome!

サインインしてコメントする。

その他の回答 (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");
plot(med.ImageNumber,med.median_Intensity_MeanIntensity_Alexa488,'k-','LineWidth',2);
hold off
  2 件のコメント
Nicholas Scott
Nicholas Scott 2022 年 3 月 9 日
This is very succinct! I definitely will have to review these different functions to see how you are managing to do this in such a compact manner, a lot is going on here, you don't even need to use a for loop to iterate over the different objects within the table!
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by