Accessing data from same variables within different tables in a structural array

Hi, I am fairly new at this, so please bear with me...
I have created a struct 1x20 containing twenty 87x6 tables, each containing data from different patients in a study (n=1:20).
Each table is made up of the same 6 variables: TimeAge, TimeCooling, TempCore, etc.. (others really don't matter).
p(n).data.TimeCooling
p(n).data.TempCore
TimeAge and TimeCooling are both in hours and each range from approximately 0 to 85.
How can I access the data from the tables for specific time points?
I would like to find the mean of the variable TempCore of all patients at the same timepoint. (i.e. I want to know the mean of all patients at TimeCooling = 1 hour, then at 2 hours, etc.)
There is some missing data in TempCore that may need to be accounted for.
Any help would be greatly appreciated!

 채택된 답변

Matt J
Matt J 2024년 6월 11일
편집: Matt J 2024년 6월 11일
fcn=@(p) interp1( p.data.TimeCooling , p .data.TempCore, timepoints(:));
TempCore_Lookups= cell2mat(arrayfun(fcn,p,'uni',0))
result = mean( TempCore_Lookups ,2,'omitnan')

댓글 수: 6

Hi Matt J,
Thanks for your response. Unfortunately, it isnt working and I'm getting this error:
Unrecognized function or variable 'timepoints'.
Error in HIE_Code>@(p)interp1(p.data.TimeCooling,p.data.TempCore,timepoints(:)) (line 129)
fcn = @(p)interp1(p.data.TimeCooling, p.data.TempCore, timepoints(:));
If I make a variable (not sure if this is the right approach):
timepoints = 1:85
then I get this error:
Error using interp1>reshapeAndSortXandV (line 438)
X must be a vector of type double or single.
Error in interp1 (line 128)
[X,V,orig_size_v] = reshapeAndSortXandV(X,V);
Error in HIE_Code>@(p)interp1(p.data.TimeCooling,p.data.TempCore,timepoints(:)) (line 130)
fcn = @(p)interp1(p.data.TimeCooling, p.data.TempCore, timepoints(:));
Matt J
Matt J 2024년 6월 11일
편집: Matt J 2024년 6월 11일
We need p to be able to reproduce your test. I suggest attaching it in a .mat file.
Hi Matt J,
I have attached a simplified/abbridged version of p.
Hopefully this helps to figure out the issue.
load p
timepoints = 1:85;
TempCore_Lookups= cell2mat(arrayfun( @(p) process(p, timepoints) ,p,'uni',0)) ;
result = mean( TempCore_Lookups ,2,'omitnan');
plot(timepoints,result)
function out=process(p, timepoints)
%ExtractTime/Temp data from a single structure element, p, and interpolate
%the data at given timepoints.
x=p.data.TimeCooling;
y=p.data.TempCore;
fin=isfinite(x)&isfinite(y);
x=x(fin); y=y(fin); %Remove NaNs (and other nonfinite data)
[x,i]=unique(x); %Remove duplicate x-values and sort
y=y(i); %Obtain corresponding y-values
out=interp1(x,y,timepoints(:)); %Interpolate (x,y) at the specitified time points
end
Thank you so much!!
Would you be able to explain a bit of what you did there so I could apply this myself in future?
Thank you so much!!
You are quite welcome, but please Accept-click the answer to indicate that it did what you want.
Would you be able to explain a bit of what you did there so I could apply this myself in future?
I've added some comments to the code to help understand what it is doing.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 6월 10일

편집:

2024년 6월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by