필터 지우기
필터 지우기

Accessing and synchronizing timetables in cell array?

조회 수: 19 (최근 30일)
Mikaela Martiros
Mikaela Martiros 2023년 2월 6일
댓글: Mikaela Martiros 2023년 2월 7일
I've been working on a script to examine salinity around specific time ranges around high tide (e.g. +/- 30 min, 1 hour, 2 hours, etc), and I'm trying to keep it as systematic as possible so that I can import different dates/locations and run it without any changes. Right now I'm at the point where I have a 296x1 cell array (mr2), with a 5x1 timetable in each cell. Essentially, I'm trying to extract the timetables from this cell array to create one combined timetable. Each timetable has the same variable, and the timestamps are chronological going through cells 1:296. I've been attempting this through a for loop, although I think I'm having an issue with the indexing.
%% Convert cell array to timetable
lengthmr = length(mr2).*5;
iterations = zeros(lengthmr,1);
sz = [lengthmr 2];
varTypes = ["datetime","double"];
varNames = ["Timestamp","Sal_psu"];
T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
for i = 1:length(iterations)
T(i,"Sal_psu") = mr2{i,1}(i,1);
end
The error message I receive is:
Error using ()
Row index exceeds table dimensions.
Error in COMPARE_salinity_model_MRB_v2m (line 46)
T(i,"Sal_psu") = mr2{i,1}(i,1);
So I know the indexing I'm doing is quite off, but I've been trying to troubleshoot for a few days now and thought I'd reach out to the community. I will also attach the variables to run this bit of code.
TIA!
  댓글 수: 1
Mikaela Martiros
Mikaela Martiros 2023년 2월 6일
Also, apologies if this is a bit clunky! I tend to write scripts by working through my logic step by step, so I often have a lot of variables or a lot of steps for fairly simple processes. I usually work these out after getting what I need from the code, but yeah, be gentle!

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

채택된 답변

Stephen23
Stephen23 2023년 2월 7일
편집: Stephen23 2023년 2월 7일
Do not use a loop for this! The simple MATLAB approach is to use a comma-separated list:
For example:
S = load('salinity_code_troubleshoot.mat');
mr2 = S.mr2
mr2 = 296×1 cell array
{5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable}
T = vertcat(mr2{:}) % comma-separated list
T = 1480×1 timetable
Timestamp Sal_psu ___________________ _______ 2022-05-12 19:30:00 6.25 2022-05-12 19:45:00 7.09 2022-05-12 20:00:00 7.78 2022-05-12 20:15:00 8.2 2022-05-12 20:30:00 8.37 2022-05-13 08:00:00 6.54 2022-05-13 08:15:00 7.13 2022-05-13 08:30:00 7.82 2022-05-13 08:45:00 8.04 2022-05-13 09:00:00 8.1 2022-05-13 20:30:00 8.63 2022-05-13 20:45:00 9.38 2022-05-13 21:00:00 9.82 2022-05-13 21:15:00 10.08 2022-05-13 21:30:00 10.22 2022-05-14 08:45:00 6.32
  댓글 수: 2
Luca Ferro
Luca Ferro 2023년 2월 7일
way better solution than mine, thank you!
Mikaela Martiros
Mikaela Martiros 2023년 2월 7일
Beautiful! Always learning, thank you for the solution and documentation links!

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

추가 답변 (1개)

Luca Ferro
Luca Ferro 2023년 2월 7일
편집: Luca Ferro 2023년 2월 7일
[cMax,~]=size(mr2);
C=mr2{1} %needs the first 5 elements of the table as preallocation
for ii=1:cMax
C=[C ; mr2{ii}]; %vertically concatenate tables
end
C=sortrows(C); %the data in mr2 is not sorted timewise, so if you want it sorted add this line
Hopefully this is what you were looking for.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by